Forum Moderators: open

Message Too Old, No Replies

Getting IDs of CLASSed DIVs

         

ahmedtheking

8:44 pm on Jan 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is it possible to get the IDs of DIVs from a class of them? If so, how would it be done?

mrhoo

10:38 pm on Jan 22, 2007 (gmt 0)

10+ Year Member



Usually you wil want the elements themselves, not their ids:

function shareClass(tag,cn){
var A=document.getElementsByTagName(tag);
var L=A.length, B=[], tem;
for(var i=0;i<L;i++){
tem=A[i];
if(tem.className.indexOf(cn)!=-1)B.push[tem);
}
return B;
}

If for some reason you want an array of ids and not elements
change this line:
if(tem.className.indexOf(cn)!=-1)B.push[tem);
to this:
if(tem.className.indexOf(cn)!=-1)B.push[tem,id);