ie win and getElementById()

C

cfeldmann

Hi.
I've got a div I insert into a document with script , assigning it an
id with div.setAttribute() so I can grab a reference to it with
getElementById() and manipulate its classname. This works like a charm
in mozilla and safari. In ie5+, the getElementById returns null. I've
made sure the id isn't duplicated and also checked that the div
actually is successfully inserted with the correct id attribute.
Actually, I'm not sure where else to look. Any suggestions?
 
W

web.dev

Hi.
I've got a div I insert into a document with script , assigning it an
id with div.setAttribute() so I can grab a reference to it with
getElementById() and manipulate its classname. This works like a charm
in mozilla and safari. In ie5+, the getElementById returns null. I've
made sure the id isn't duplicated and also checked that the div
actually is successfully inserted with the correct id attribute.
Actually, I'm not sure where else to look. Any suggestions?

Hi cfeldmann,

In IE, the setAttribute method does not work properly. A work around
for what you're trying to do is simply doing the following instead:

div.id = "myId";
 
C

cfeldmann

Hi.
This is something I hadn't actually tried, so I did this and also
div["id"] = "id", but neither seems to change the result. In all three
cases (these two and setAttribute()), I can dump(div.nodeName +
node.getAttribute("id")) immediately afterward and see that it is
successfully created and appended to the tree. It's just gone later
when I try to get it with a different chunk of seperately loaded script.
 
G

Gérard Talbot

(e-mail address removed) a écrit :
Hi.
This is something I hadn't actually tried, so I did this and also
div["id"] = "id", but neither seems to change the result.


var objDiv = document.createElement("div");
objDiv.id = "asdf";

should work in all browsers, even MSIE 5.x.
http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-63534901

In all three
cases (these two and setAttribute()), I can dump(div.nodeName +
node.getAttribute("id")) immediately afterward and see that it is
successfully created and appended to the tree.


You also need to check the name attribute because MSIE mixes names and
id attribute values in the collection list (that's a spec violation by
MSIE 5+ actually). If there is another element with the same attribute
value, then MSIE will render the first encountered.

E.g.: <input name="asdf" ...> will interfere with return value of
document.getElementById("asdf") depending on the place of the call and
the respective places of other elements.

e.g.:

<input name="asdf" ...>
(...)
<div id="asdf" ...>

and here if you query document.getElementById("asdf"), then the input
element will be fetched, not the div.


It's just gone later
when I try to get it with a different chunk of seperately loaded script.

Your problem may depend on how your whole page DOM tree is built and/or
how you've coded all this. You have not provided an url for the webpage
so that we could examine the issue. So it's not possible to know for
sure what is the problem..

Gérard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,002
Messages
2,570,261
Members
46,858
Latest member
FlorrieTuf

Latest Threads

Top