Events

This is what I have:
http://page-test.co.uk/js-test.html

…and it works exactly how I want it to, but I need it to work by doing these two things:

(a) If you see in the change_button function, I have hard coded the ‘name_can_vary’ name element. I need to do it so this isn’t hard coded (so it can work on any page/form).

(b) I don’t want to change any of the HTML, and I only want to change the code used in the change_button function (otherwise I will need to change many pages).

Thanks in advance.

Let’s look at the form:


<form autocomplete="off" method="POST" class='form-name'>
    <button type="submit" name="name_can_vary">Text Can Vary</button>
</form>

and the relevant scripting code is:


function change_button(evt) {
    evt = evt || window.event;
    var targ = evt.target || evt.srcElement;
    targ.name_can_vary.innerHTML = "CHANGED - OK";
    ...
}

You have associated a function with the form onsubmit event, which means that you cannot easily gain access to the button that was used to trigger the event.

Currently your code is retrieving the target that the event takes place on. Since the onsubmit event cannot take place on a button, but only with the form, it is the form that is the target of your onsubmit event.

What this means is, is that the targ variable of your code is just the same as the this keyword, both being the form itself. So you could instead use this to get the element with a submit type:


this.querySelector('[type="submit"]).innerHTML = "CHANGED - OK";