Dynamically Assign a Funtion
If you’ve never had a situation where you’ve had to dynamically assign a function to an event handler, you are fortunate. This isn’t necessarily the most common of tasks, but, I have encountered it.
Lets say we have assigned the onsubmit handler to a function named validateForm_1. Now, let us also assume we have several submit buttons. For each of these button you may wish to assign a different function to the onsubmit event. Why? Well, I don’t know. In the case I was working on, it was simply bad code handed to me. Hey… You do what you gotta, eh?
Special thanks to Mark DeMoss in helping on this one. Dude has a huge stash of arcane knowledge!
-
<form onsubmit="validateForm_1">
-
<input type="submit" onclick="this.form.onsubmit = new Function(’return validateForm_2;’);" />
-
<input type="submit" onclick="this.form.onsubmit = new Function(’return validateForm_3;’);" />
-
<input type="submit" onclick="this.form.onsubmit = new Function(’return validateForm_4;’);" />
-
</form>