473,761 Members | 3,542 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

createElement and getElementsByNa me not working in IE?

It seems like if you create an element dynamically with createElement()
and then try to get it later with getElementsByNa me()
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.getEle mentById("theSp an");

var cb = document.create Element("input" );
cb.type = "checkbox";
cb.name = "theCheckbo x";

s.appendChild(c b);

var names = document.getEle mentsByName("th eCheckbox");

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 12706
P.S. That's Firefox 1.0.6 and IE 6.0.2900.

Nov 23 '05 #2
> var names = document.getEle mentsByName("th eCheckbox");

hmmm! I'm not sure about the method getElementsByNa me - shouldn't that be
getElementsByTa gName

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

Brian

Nov 23 '05 #3
getElementsByNa me() and getElementsByTa gName() 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************ ******@bstaffw2 k.jda.corp.loca l>
<11************ **********@g49g 2000cwa.googleg roups.com>
Newsgroups: microsoft.publi c.scripting.jsc ript,comp.lang. javascript
NNTP-Posting-Host: gw.jda.com 65.160.56.2
Path: border1.nntp.dc a.giganews.com! nntp.giganews.c om!newshub.sdsu .edu!msrtrans!T K2MSFTNGP08.phx .gbl!TK2MSFTNGP 10.phx.gbl
Lines: 1
Xref: number1.nntp.dc a.giganews.com microsoft.publi c.scripting.jsc ript:83090 comp.lang.javas cript: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...inte restingly, your code works if you
add one line:

cb.id = "theCheckbo x";

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
getElementsByNa me() it's found in Firefox but not in IE.
Yes, that is one of those IE bugs.

<snip> function addCB(){
var s = document.getEle mentById("theSp an");

var cb = document.create Element("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.theChe ckbox[namedEls.theChe ckbox.length] = cb;

- and then - namedEls.theChe ckbox - 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2599
by: Pieter Van Waeyenberge | last post by:
Hello, im some image elements using doc.createElement('IMG'); i set the following styles: - for the images container: white-space:normal; - for the images themselves: display: inline; i append the images to the body ... but the imagery dont wrap when the window gets smaller, unless i append a space (createTextNode) after each image
5
3689
by: simon | last post by:
<html> <head> <script language="vbscript"> sub window_onload msgbox document.getElementsByName("name1").length end sub </script> </head> <body> <div name="name1">testDiv</div>
2
5701
by: André Wagner | last post by:
I'm trying to get all the "divs" that have a given NAME using getElementsByName(). For example, the following code: <html> <head> <script type="text/javascript"> function on_load() { var pages = document.getElementsByName("name");
22
7975
by: Saul | last post by:
I have a set of radio buttons that are created dynamically, after rendered I try loop thru this set by getting the length of the set, but I keep getting an error stating the element is undefined. I am using getElelementsByName since these are radio buttons, but it seems that the dynamic element is not seen!!! This is my code... please let me know if there is anything that I am doing wrong! - thanks ---- ....
4
7976
by: david.kuczek | last post by:
I got the following error in a javascript I wrote. The script works fine, but why is the error being displayed??? ##### Here comes the little script: function chgMusicLink(userid,userlink,usertype) { var card_music_id = document.getElementsByName(userid).value; document.getElementsByName(userlink).href="/card_music_preview.tcl?card_music_id="+card_music_id+"&card_music_type="+usertype; }
3
4011
by: acecraig100 | last post by:
I am fairly new to Javascript. I have a form that users fill out to enter an animal to exhibit at a fair. Because we have no way of knowing, how many animals a user may enter, I created a table with a createElement function to add additional entries. The table has the first row of input text boxes already in it. You have to click a button to add another row. That seems to be working fine. How do I pull the information from the input boxes...
5
6393
by: kurzweil4 | last post by:
In the below code, Firefox displays 2 on the dialog, but Internet Explorer returns 0. Any ideas why this is not working? Thanks in Advance, Kurzweil4 <html> <head> <script type="text/javascript"> function doIt() {
13
16216
Atli
by: Atli | last post by:
Hi everybody. I'm making a Javascript system to build tables, so I can view large tables in smaller pieces. I use the "document.createElement" and "appendChild" methods to create and remove elements from my table. Got everything working, no problems. Until I test my code in IE. After a little investigation, I create this: <div id="ListContainer" class="Info"> <table id="ListTable"> <tr>
23
6624
by: vunet | last post by:
It is recommended by some sources I found to create IFrames in IE using document.createElement('<iframe src="#">') instead of document.createElement('iframe'). Why and what browser versions to use it? IE5 or IE6? Thanks
0
9554
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9377
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9989
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9925
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7358
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5266
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3913
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2788
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.