Wrox Home  
Search P2P Archive for: Go

  Return to Index  

javascript_howto thread: ID Collection


Message #1 by "Chris Thompson" <cthompson@n...> on Thu, 16 May 2002 14:46:34 -0600
So I have several items with the ID tag or name tag set to the same
thing (ie: "Itemnbr")?  Or do the all have the same name but different
ID's?

Chris


-----Original Message-----
From: Sten Hougaard [mailto:STG@e...]
Sent: Friday, May 17, 2002 12:25 AM
To: JavaScript HowTo
Subject: [javascript_howto] Re: ID Collection


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 =3D document.getElementsByName(sTagName);
  var aItems =3D new Array();
  for(var i=3D0; i<cInput.length; i++) {
    if (cInput[i].name =3D=3D sAttributeName) {
      aItems[aItems.length] =3D cInput[i];
    }
  }
  return (aItems);
}

var myItemCollection =3D 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=3Dnosim/theprogramm
e
r-20
Constructing Accessible Web Sites
http://www.amazon.com/exec/obidos/ASIN/1904151000/ref=3Dnosim/theprogramm
e
r-20
Practical JavaScript for the Usable Web
http://www.amazon.com/exec/obidos/ASIN/1904151051/ref=3Dnosim/theprogramm
e
r-20





---

Improve your web design skills with these new books from Glasshaus.

Usable Web Menus
http://www.amazon.com/exec/obidos/ASIN/1904151027/ref=3Dnosim/theprogramm
e
r-20
Constructing Accessible Web Sites
http://www.amazon.com/exec/obidos/ASIN/1904151000/ref=3Dnosim/theprogramm
e
r-20
Practical JavaScript for the Usable Web
http://www.amazon.com/exec/obidos/ASIN/1904151051/ref=3Dnosim/theprogramm
e
r-20

  Return to Index