INFO: Text Selection in a Control Not Cleared on Blur Event (231321)



The information in this article applies to:

  • Microsoft Internet Explorer (Programming) 5
  • Microsoft Internet Explorer (Programming) 5.5

This article was previously published under Q231321

SUMMARY

Generally it is expected that the selection in a control is cleared when the focus moves to a different control. But Internet Explorer 5.0 does not behave this way, enabling toolbar-like scenarios, such as clicking on the button, to manipulate the selection.

MORE INFORMATION

The following code demonstrates how to clear the text when the focus moves to another control, by setting the control to its own value in the onblur event.

Place the following code in an HTML file and view in Internet Explorer.

<HTML> 
<HEAD> 
<SCRIPT LANGUAGE=vbscript> 
<!-- 

Sub window_onload 
	text1.value = "test" 
	text1.select 
End Sub 

Sub text1_onfocus 
	text1.select 
End Sub 

Sub text1_onblur 
	text1.value = text1.value 
End Sub 

Sub text1_onkeydown 
	Select Case window.event.keyCode 
		Case 13  '-Enter 
			button1.focus 
		Case Else 
	End Select     
End Sub 

Sub button1_onkeydown 
	Select Case window.event.keyCode 
		Case 13  '-Enter 
                        text1.focus 
                Case Else 
	End Select     
End Sub 

--> 
</SCRIPT> 
</HEAD> 

<BODY bgcolor=paleturquoise> 

<P> 
When setting focus from a text box to a button in Internet Explorer 5, the
text that was selected remains selected even though the focus is now on the
button.
</P> 

<P>
To work around this, set the value of contents of the Text to itself.
This can be done by uncommenting code in <EM>onblur</EM> event of text1.
</P>

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

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

				

REFERENCES

For more information about developing Web-based solutions for Microsoft Internet Explorer, visit the following Microsoft Web sites:

Modification Type:MajorLast Reviewed:5/11/2006
Keywords:kbDHTML kbinfo KB231321