Microsoft KB Archive/231322

= INFO: Override a Default Action in Internet Explorer 5 by Canceling the Event =

Article ID: 231322

Article Last Modified on 5/11/2006

-

APPLIES TO


 * Microsoft Internet Explorer 4.0 128-Bit Edition
 * Microsoft Internet Explorer 4.01 Service Pack 2
 * Microsoft Internet Explorer 4.01 Service Pack 1
 * Microsoft Internet Explorer 4.01 Service Pack 2
 * Microsoft Internet Explorer 5.0
 * Microsoft Internet Explorer 5.5

-



This article was previously published under Q231322



SUMMARY
You can override an event's default action by setting the returnValue property on the window.event object to false. This will cancel the event.



MORE INFORMATION
For example, the default action of pressing the TAB key on an HTML page is to move the caret to the next item in the tabindex. With Internet Explorer 4, you could set the focus to whichever element you wanted without setting the event object's returnValue. In Internet Explorer 5 you need to set it. This is also the best way to cancel a form submission in Internet Explorer 4 and 5.

To cancel the default action, you need to set window.event.returnValue=false inside your event.

The following code demonstrates how to cancel the default action. Place this code in an HTML file and view in Internet Explorer.     



 When pressing  while in the text box in Internet Explorer 5, the next text box is getting the focus even though the code directs the focus to the button.   You need to cancel the event to override the default action (which, in this case, is to set focus to the next item in the tabindex). Merely overriding the event and setting focus elsewhere does not prevent the default action in Internet Explorer 5. To cancel the default behavior, you need to set window.event.returnValue = false inside your event.



 Text1 <INPUT id=text1 name=text1 tabindex=1> Text2 <INPUT id=text2 name=text2 tabindex=2></P>

 Button <INPUT id=button1 name=button1 type=button value=Button tabindex=3> </P> </BODY> </HTML>

<div class="references_section">