469,657 Members | 1,822 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,657 developers. It's quick & easy.

createElement and getElementsByName not working in IE?

It seems like if you create an element dynamically with createElement()
and then try to get it later with getElementsByName()
it's found in Firefox but not in IE. Here's an example program.

Is there something wrong with the program, or is there another way to
get elements that have been created dynamically? Thanks.

<html>
<body>

<SCRIPT>
function addCB(){
var s = document.getElementById("theSpan");

var cb = document.createElement("input");
cb.type = "checkbox";
cb.name = "theCheckbox";

s.appendChild(cb);

var names = document.getElementsByName("theCheckbox");

var nameStr = (names) ? names.length : "null";

alert("names=" + nameStr);

}

</SCRIPT>

<SPAN ID="theSpan" onClick = "addCB()" > click here </SPAN>

</body>
</html>

Nov 23 '05 #1
6 12526
P.S. That's Firefox 1.0.6 and IE 6.0.2900.

Nov 23 '05 #2
> var names = document.getElementsByName("theCheckbox");

hmmm! I'm not sure about the method getElementsByName - shouldn't that be
getElementsByTagName

I would make sure the node has an ID and then use getElementById

Brian

Nov 23 '05 #3
getElementsByName() and getElementsByTagName() are 2 different
functions, see

http://msdn.microsoft.com/library/de...ce/methods.asp

I can't use getElementById in my real program because there will be
several of them
and you can't give the same ID to more than one element.

But thanks for the answer,

- Mitch

Nov 23 '05 #4
<VA******************@bstaffw2k.jda.corp.local>
<11**********************@g49g2000cwa.googlegroups .com>
Newsgroups: microsoft.public.scripting.jscript,comp.lang.javas cript
NNTP-Posting-Host: gw.jda.com 65.160.56.2
Path: border1.nntp.dca.giganews.com!nntp.giganews.com!ne wshub.sdsu.edu!msrtrans!TK2MSFTNGP08.phx.gbl!TK2MS FTNGP10.phx.gbl
Lines: 1
Xref: number1.nntp.dca.giganews.com microsoft.public.scripting.jscript:83090 comp.lang.javascript:526713
I can't use getElementById in my real program because there will be
several of them
and you can't give the same ID to more than one element.


But (I think) that's the whole point of ID - they are supposed to be unique
which is why getElementById returns just a single node. NAME on the other hand,
can have duplicates (as in RADIO buttons).

Maybe think of coding "dups" like this:

<input id='r1' name='radio' type='radio' value='r1' />
<input id='r2' name='radio' type='radio' value='r2' />

But in reply to your original question...interestingly, your code works if you
add one line:

cb.id = "theCheckbox";

Brian

Nov 23 '05 #5
mitch wrote:
It seems like if you create an element dynamically with
createElement() and then try to get it later with
getElementsByName() it's found in Firefox but not in IE.
Yes, that is one of those IE bugs.

<snip> function addCB(){
var s = document.getElementById("theSpan");

var cb = document.createElement("input");

<snip>

At this point you have a reference to the element. Instead of throwing
this reference away you could hang on to it, and accumulative it with
references to the other like-named elements you create and then you
would not need to have the browser go searching for it later. I.E:-

Globally:-

var namedEls = {theCheckbox:[]};

- and upon creation:-

namedEls.theCheckbox[namedEls.theCheckbox.length] = cb;

- and then - namedEls.theCheckbox - will be an array of the elements you
create under the same name and may empoyed by any code that needs it.

Richard.
Nov 23 '05 #6
> I can't use getElementById in my real program because there will be
several of them
and you can't give the same ID to more than one element.


maybe put them in an array when you create them.
Bye.
Jasen
Nov 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Pieter Van Waeyenberge | last post: by
5 posts views Thread by simon | last post: by
2 posts views Thread by André Wagner | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.