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
2009-11-18 12:36:30
Uhm, `++i` instead of `i++` ?
BalusC
2009-11-18 12:43:07
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
2009-11-18 12:54:12
+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
2009-11-18 15:35:10