View Full Version : Using Classes
scriptkeeper
08-13-2003, 01:31 AM
Hey all, I know you can reference certain elements buy its ID many different ways! Is it possiblle to do the same with class names? I know I have seen javascript manipulating class names somewhere, .className or something? Thanxs 4 your time1
glenngv
08-13-2003, 03:41 AM
document.getElementById("id").className
className is read/write property
scriptkeeper
08-13-2003, 04:42 PM
So you have to already know the element you can not reference by class names?
You mean a generic way...
I am not quite sure, but you can use
document.getElementById[0].className
Adam20002
08-14-2003, 12:20 PM
Or maybe this http://simon.incutio.com/archive/2003/03/25/#getElementsBySelector might help.
Adam
brothercake
08-14-2003, 12:27 PM
That's a lot of code .. if you just want elements by classname and you know what tag name they are, you can iterate like that:
var tags = document.getElementsByTagName('div');
var tagsLen = tags.length;
for(var i=0; i<tagsLen; i++)
{
if(tags[i].className == 'something')
{
alert("this one!");
}
}
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.