I have run into a bug with IE-Win several times where it will throw the following JavaScript error:
[B]Can’t move focus to the control because it is invisible, not enabled, or of a type that does not accept the focus.
Code: 0
Tonight I finally found what causes it and below is a quick code sample of how the error is encountered:
[code=html]<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01//EN”
“http://www.w3.org/TR/html4/strict.dtd”>
<html>
<head>
<title>Internet Explorer Focus Error</title>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″>
<script type=”text/javascript”>
<!– begin hiding
function setFocus(objId) {
document.getElementById(objId).focus();
return false;
}
// end hiding –>
</script>
</head>
<body>
<form action=”#” method=”get”>
<a href=”#” onclick=”return setFocus(‘myTextBox’);”>set focus —></a>
<input type=”text” size=”20″ name=”myTextBox” id=”myTextBox2″ style=”display: none”>
</form>
<form action=”#” method=”get”>
<input type=”text” size=”20″ name=”myTextBox” id=”myTextBox”>
</form>
</body>
</html>
I had forgotten that Internet Explorer doesn’t impliment the document.getElementById function properly, and that it’s treated more like document.getElementByIdAndOrGetElementsByName();
In the code above, you’ll notice that two text boxes exist, both with the same name of “myTextBox” in two separate forms. According to the HTML specs, this is Ok. They are in separate forms. One text box is given the Id of “myTextBox2”. The second text box — which has the same name as the first — has an Id of “myTextBox”. In Internet Explorer, document.getElementById(“myTextBox”) incorrectly returns a node reference to the first text box [B]named
The second part is that the first text box (named myTextBox but given an Id of myTextBox2) is set to display: none. In Internet Explorer you cannot set focus to any element whose display is none. SO! To sum this up:
getElementById that shares a name attribute with another element +
that other element being invisible +
big fat javascript error in Internet Explorer.
This error, in one form or another, has been plaguing me for months so I thought I’d share it. ?