Jump to content

getElementsByName


jeffman

Recommended Posts

Aargh. For the first time, I tried using getElementsByName as it was intended, to identify a related group of elements so I could operate on them as a group. Works great in Firefox. Nothing in MSIE7. When I checked the array the method is supposed to return, the length is 0. Googled the problem. Found some comments saying the method doesn't work in IE.Is this correct?

Link to comment
Share on other sites

getElementsByName, to my knowledge, isn't exactly a standard.What I did once to get around it was to put all the elements I was planning to refer to in a tag I wasn't going to use for anything else (in my case, <ins>), and then style it with CSS to make up for any differences.Another thing would be to start looping through all the elements of one tagName and selecting them if their name attribute is the desired value:

G = document.getElementsByTagName("div");for(x=0;x<g.length;x++) {  if(G[x].getAttribute("name") == "value") {	// perform an action  }}

Link to comment
Share on other sites

Thanks for the work-around. I already had one (basically yours; I chose className as the attribute) but other readers will appreciate seeing yours. I dug a little deeper into w3.org, and I found a fair amount of support for the method in DOM 1, but by DOM 2 the web gods had shifted their interest to getElementsByTagName.Still, it's odd that IE7 recognizes the method (no errors) but doesn't use it correctly. I'd have debugged more quickly if the browser hadn't recognized the method at all. I hope readers get something out of that experience too.

Link to comment
Share on other sites

Hey!There's a chance here that MS is deprecating the 'name' form of id. I'm pretty sure I read that in at least one of the tutorials on HTML. That also explains why the use of id="label" and name="label" are combined on the same command line and thus is used redundantly throughout the HTML programming experience.Well, from all the reading I've been doing, that seems logical to me! :)Lord Cupid

Link to comment
Share on other sites

I hope MS doesn't deprecate any tags or attributes - that is up to the W3C to decide! However, the name attribute is going out of favour against the id attribute, but it isn't deprecated yet as it is needed in forms.

Link to comment
Share on other sites

I hope MS doesn't deprecate any tags or attributes - that is up to the W3C to decide! However, the name attribute is going out of favour against the id attribute, but it isn't deprecated yet as it is needed in forms.
Yeah, about that... I've noticed with PHP, you could put "[]" in a form field's name to indicate an array. ID seems to forbid those characters. That is, even if you have "field[0]" and "field[1]", your XHTML becomes invalid because of the "[" and "]", despite the fact the IDs are still unique. Now, am I the only one that feels "name" will be here for a long time?
Link to comment
Share on other sites

Name and ID have two different uses. ID references an element in the DOM. Name gives the element a name to use when it gets submitted to the server. There's no reason those need to or should be the same things, I might have 5 elements that all have the name of "mode" even though they have different IDs.

Link to comment
Share on other sites

Name and ID have two different uses. ID references an element in the DOM. Name gives the element a name to use when it gets submitted to the server. There's no reason those need to or should be the same things, I might have 5 elements that all have the name of "mode" even though they have different IDs.
It's about time someone clarified that for me!My mind tends to wander when I read something like: Use both ID & name elements in your naming of an element. That may not be exactly what I read on W3Schools.com, however it's close enough and of coure, Why use both? Unless maybe one is on its way out in favor of the other? Thus the Microsoft thing.Again, appreciate the logical explanation!Grace, Peace & Love;Lord Cupid
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...