Microsoft KB Archive/190247

= How To Change the Function an Event Handler Points To =

Article ID: 190247

Article Last Modified on 8/8/2007

-

APPLIES TO


 * Microsoft JScript 1.0
 * Microsoft JScript 2.0
 * Microsoft JScript 3.0
 * Microsoft Internet Explorer 5.0
 * Microsoft Internet Explorer 5.5

-



This article was previously published under Q190247



SUMMARY
Sometimes you may want to change the code that an event handler is pointing to. To do this the function that the event handler points to must change. VBScript does not support this, but JScript does.



MORE INFORMATION
In the following example, the next time this button is pressed, you want to invoke this function with a different value for arguments. This function should get called with the value "value 2." The following code demonstrates two intuitive but incorrect methods of doing this. The two methods are commented.

  Changing event handlers at run-time 

    

The following three lines build a string for use with the Function object in JScript and will change the handler function that onClick points to. Replace the two commented statements above with these three statements to correctly change the handler function.

var newHandlerArg = "value 2" ; var newFunctionBody = "test('" + newHandlerArg + "');" ; btn1.onclick = new Function(newFunctionBody);

