getElementsByClassName

getElementsByClassName is defined in HTML5. From WhatWG:

The getElementsByClassName(classNames) method takes a string that contains a set of space-separated tokens representing classes.

Given the following XHTML fragment:

<div id="example">
 <p id="p1"/>
 <p id="p2"/>
 <p id="p3"/>
</div>


A call to document.getElementById(‘example’).getElementsByClassName(‘aaa’) would return a NodeList with the two paragraphs p1 and p2 in it.

A call to getElementsByClassName(‘ccc bbb’) would only return one node, however, namely p3.

A call to document.getElementById(‘example’).getElementsByClassName(‘bbb ccc ‘) would return the same thing.

A call to getElementsByClassName(‘aaa,bbb’) would return no nodes; none of the elements above are in the “aaa,bbb” class.

Firefox 3 does support getElementsByClassName. IE8 does not. Could not find if it’s implemented in IE9.

Leave a Reply

Your email address will not be published.