tags:

views:

79

answers:

2

let's say I selected a bunch of elements by class. how do i find out what the ID is of each of my returned elements?

+7  A: 

Access the id property

for (var i = 0; i < elements.length; ++i) {
    elements[i].id
}

or

for (var i = 0; i < elements.length; ++i) {
   elements[i].getAttribute('id');
}
Zoidberg
Uhm, `++i` instead of `i++` ?
BalusC
Meh, whatever you prefer, I have heard that ++i is a little more efficient than i++, but I don't think it really matters all that much.
Zoidberg
+1  A: 

Try this :

 function selectedDivs (theClass) {
             var allHTMLTags=document.getElementsByTagName("div");

            for (i=0; i<allHTMLTags.length; i++) {
            //Get all tags with the specified class name.
            if (allHTMLTags[i].className==theClass) {
            allHTMLTags[i].getAttribute('id');
            }
            }
            }
c0mrade