Your getElementsByName function should use document.getElementsByTagName
rather than document.getElementsByName in this implementation. The
modified function is:
function getElementsByName(sTagName, sAttributeName) {
var cInput = document.getElementsByTagName(sTagName);
var aItems = new Array();
for(var i=0; i<cInput.length; i++) {
if (cInput[i].name == sAttributeName) {
aItems[aItems.length] = cInput[i];
}
}
return (aItems);
}
-David S
>
Hi,
I believe that an ID should be unique - one id for one element.
If you have several of the same ID, then you have a document
which is not well structured.
Instead use NAME cause it is allowed to have several elements
with the same name.
Say you have some order lines which all are INPUT tags. They
might have the name "Itemnbr". One way to get a collection/array of them
then is to do like this:
function getElementsByName(sTagName, sAttributeName) {
var cInput = document.getElementsByName(sTagName);
var aItems = new Array();
for(var i=0; i<cInput.length; i++) {
if (cInput[i].name == sAttributeName) {
aItems[aItems.length] = cInput[i];
}
}
return (aItems);
}
var myItemCollection = getElementsByName('input', 'Itemnbr');
This will get the INPUT tags with the NAME 'Itemnbr' into the var
myItemCollection.
In it you have references to the elements - so myItemCollection[0].value
will return
the value of the first Itemnbr input field of your page! :-)
Cheers
Sten Hougaard
EDB Gruppen
Application developer/web-designer
e-business solutions
"Chris
Thompson" To: "JavaScript HowTo"
<javascript_howto@p...>
<cthompson@n...
cc:
tah.com> Subject: [javascript_howto]
ID Collection
16-05-
2002
22:46
Please
respond
to "JavaScript
HowTo"
Is there a collection of ID's?
For example, if you have 5 objects with an ID, then can you get a
collection of objects with an ID to loop through?
Thanks,
Chris
---
Improve your web design skills with these new books from Glasshaus.
Usable Web Menus
http://www.amazon.com/exec/obidos/ASIN/1904151027/ref=nosim/theprogramme
r-20
Constructing Accessible Web Sites
http://www.amazon.com/exec/obidos/ASIN/1904151000/ref=nosim/theprogramme
r-20
Practical JavaScript for the Usable Web
http://www.amazon.com/exec/obidos/ASIN/1904151051/ref=nosim/theprogramme
r-20
---
Change your mail options at http://p2p.wrox.com/manager.asp or
to unsubscribe send a blank email to