/    Sign up×
Community /Pin to ProfileBookmark

Frustrating Internet Explorer Bug When Setting Focus

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[/B]

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 —&gt;</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>
[/code]

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[/B] “myTextBox”, not the text box whose Id is “myTextBox”. That’s part of the error.

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 +

setting focus to the element returned from getElementById
=======================================================

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. ?

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@mrhooMar 15.2007 — Maybe a general rule you could learn from this is that you should give your form controls ids that are not the same as the names of other elements in the same document.
Copy linkTweet thisAlerts:
@KorMar 15.2007 — Welcome to the [I]"I_reinvent_the_wheel"[/I] club. ? That IE bug (taking the name as id) is well known. This is the reason for you should always make sure that the id of an element is really unique (there must be no other element with the same id [I]or with a name with the same value[/I]).

On the other hand, if you would have used [I]getElementsByName()[index][/I] method, you would not encounter that problem.
Copy linkTweet thisAlerts:
@toicontienauthorMar 15.2007 — Correct. Normally I don't use the same Id as a name in my HTML, but the CMS that we built has config files for creating forms and the person who wrote the config files used the name as an Id for the form control. It's a simple matter of just editing the config files, but this bug confounded me for months.
Copy linkTweet thisAlerts:
@KorMar 15.2007 — Correct. Normally I don't use the same Id as a name in my HTML, but the CMS that we built has config files for creating forms and the person who wrote the config files used the name as an Id for the form control. It's a simple matter of just editing the config files, but this bug confounded me for months.[/QUOTE]
I know the feeling. I also worked with developers which gave "arbitrary" ids and names to the elements, which gave me a lot of troubles afterwards.
×

Success!

Help @toicontien spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...