Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

How To Change the Function an Event Handler Points To


View products that this article applies to.

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.

↑ Back to the top


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.
<HTML>
<HEAD>
<TITLE>Changing event handlers at run-time</TITLE>
<script language="JScript">
<!--
function test (sArgs)
{
    alert ("I am test ()" + sArgs);

    // this actually calls the function,
    // which doesn't change it
    // and causes recursion.
    // btn1.onclick = test ('value 2')

    // this just changes the onclick
    // value to a new string,
    // but doesn't change the function.
    // btn1.onclick = "test ('value 2')"

}
-->
</script>
</HEAD>
<BODY>
<input id=btn1 type=button value="(JScript)Change function"
  onClick="test('value 1');">
</BODY>
</HTML>
				

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

↑ Back to the top


References

For further reference material on JScript, see the following Web page:

↑ Back to the top


Properties

Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.

↑ Back to the top


Keywords: KB190247, kbhowto, kbcode, kbaccess200fix

↑ Back to the top

Article Info
Article ID : 190247
Revision : 5
Created on : 8/8/2007
Published on : 8/8/2007
Exists online : False
Views : 428