[Javascript] Javascript and CSS

Scott Reynen scott at randomchaos.com
Wed Mar 26 22:24:20 CDT 2008


On Mar 26, 2008, at 7:48 PM, Paul Novitski wrote:

> You can get the class of a known element simply by referencing its  
> className:
>
>         var oExample = document.getElementById('example');
>         var sClass = oExample.className;


"The class" may be somewhat misleading; despite what the name  
suggests, className contains a space-separated list of classes, not  
just one.  So if you're testing this against a given class, you should  
either split it up first or use a regular expression.

On Mar 26, 2008, at 8:02 PM, David Hucklesby wrote:

> for getting all elements having a certain class then,
> no, there is no built-in JavaScript function.

This is also a bit misleading; Safari 3.1 and Firefox 3 both have  
getElementsByClassName built-in.

And since I'm being pedantic already, I'll go ahead and comment on the  
subject as well.  The class attribute is not exclusive to CSS.  We can  
do many useful things with classes (e.g. in JavaScript) that have  
nothing to do with CSS.

Peace,
Scott




More information about the Javascript mailing list