preventDefault
Returns: Nothing.
|
NN2 NN3 NN4 NN6 IE3/J1 IE3/J2 IE4 |
IE5 IE5.5 | |
|
Compatibility |
✓ | |
While NN6+ continues to honor the original way of preventing default action for an event handler (that is, having the last statement of the event handler evaluate to return false), the NN6+ event model provides a method that lets the cancellation of default action take place entirely within a function invoked by an event handler. For example, consider a text box that is supposed to allow only numbers be typed in it. The onKeyPress event handler can invoke a function that inspects each typed character. If the character is not a numeric character, then it does not reach the text box for display. The following validation function may be invoked from the onKeyPress event handler of just such a text box:
function checklt(evt) {
var charCode = evt.charCode if (charCode < 48 || charCode > 57) {
alert("Please make sure entries are numbers only.") evt.preventDefault()
This way, the errant character won't appear in the text box.
Invoking the preventDefault() method in NN6 is the equivalent of assigning true to event.returnValue in IE5+.
Related Items: cancelable property.
Post a comment