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.
<HTML>
<HEAD>
<SCRIPT LANGUAGE=vbscript>
<!--
Sub text1_onkeydown
Select Case window.event.keyCode
Case 9 '-Tab
'Please uncomment this line to cancel the default action
'window.event.returnValue = false
button1.focus
Case Else
End Select
End Sub
-->
</SCRIPT>
</HEAD>
<BODY bgcolor=paleturquoise>
<P>
When pressing <TAB> 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.
</P>
<P>
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 <EM>window.event.returnValue = false</EM> inside your event.
</P>
<P>
Text1
<INPUT id=text1 name=text1 tabindex=1>
Text2
<INPUT id=text2 name=text2 tabindex=2></P>
<P>
Button
<INPUT id=button1 name=button1 type=button value=Button tabindex=3>
</P>
</BODY>
</HTML>