Dynamically Assign a Funtion

October 13th, 2007
by psykoprogrammer

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! ;)

  1. <form onsubmit="validateForm_1">
  2.   <input type="submit" onclick="this.form.onsubmit = new Function(’return validateForm_2;’);" />
  3.   <input type="submit" onclick="this.form.onsubmit = new Function(’return validateForm_3;’);" />
  4.   <input type="submit" onclick="this.form.onsubmit = new Function(’return validateForm_4;’);" />
  5. </form>

Comments (0)

No comments yet

Leave a Reply

You must be logged in to post a comment.