Friday, September 3, 2010

Access the control that has invoked a method in JavaScript

Suppose that you have a function in Javascript that can be invoked by several controls. In order to access the actual control that has invoked the method, you can use the code shown here:

<input id="Button1" type="button" value="button" onclick="myfunc();"/>

function myfunc()
{
      window.event.srcElement.style.color = 'red';
}

In this example, if you click the button (or any other buttons with their onclick set to 'myfunc'), its forecolor will change to red.

No comments:

Post a Comment