By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,481 Members | 1,713 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,481 IT Pros & Developers. It's quick & easy.

ie win and getElementById()

P: n/a
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?

Sep 27 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

cf*******@gmail.com wrote:
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";

Sep 27 '05 #2

P: n/a
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.

Sep 27 '05 #3

P: n/a
cf*******@gmail.com 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-HTM...ml#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
--
remove blah to email me
Sep 28 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.