|
August 3, 2000 Using "this" in Event Handling Tips: August 2000
Yehuda Shiran, Ph.D.
|
|
When you call a function via an event handler, you may refer to the form element which triggered the event handler, such as a text field or a button. Every form element is an object. You can use the keyword this to refer to the "current" object. The following entry field initially displays the string "email..." but changes to "thank you" once you modify it (click outside the field to signal the change's end):There are two ways to implement this effect, with and without this. The following script refers to a form element without using the this keyword (this script was used for the field above):
We can implement the same effect with the Here is the script:
This form is preferred because you can change the element's position, name, or any other optional attribute (other than the event handler) and the script will still work without modifications. The keyword
People who read this tip also read these tips: Look for similar tips by subject: |