| JavaScript - Event handlers as JavaScript functions | | Print | |
Event handlers as JavaScript functions
We've seen that the various document object models represent HTML tags as JavaScript objects, with the attributes of those tags as properties of the objects. The same is true of event handlers. If your HTML document includes a single <form> tag with an onsubmit event handler attribute, that event handler is available as:
document.forms[0].onsubmit
Although HTML event handler attributes are written as strings of JavaScript code, the value of the corresponding JavaScript properties are not strings of code, but actual JavaScript functions. You can create a new event handler simply by assigning a function to the appropriate property:
function validate() { // Form validation function
// check validity here
return valid; // return true or false
}
// Now check user input before submitting it
document.forms[0].onsubmit = validate;
This entry was posted on . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a comment.
| Users' Comments (0) |
|
No comment posted





