What's new? | Help | Directory | Sign in
Google
             
Search
for
Updated Nov 11, 2008 by pilgrim
Labels: about-dom, is-dom-method
DocumentGetElementsByClassNameMethod  
The document.getElementsByClassName method

The getElementsByClassName method returns a list of elements in the DOM that define one or more classnames in the class attribute. getElementsByClassName() exists as a method of the document object (for searching the entire DOM), as well as on each HTMLElement object (for searching the children of an element).

Arguments

The getElementsByClassName method takes a single argument, a string, which is the classname you wish to match; it returns a NodeList, i.e. an array of elements that define the given classname. If the argument contains spaces, it is treated as a space-delimited array of classnames (thus matching the syntax of the `class` attribute itself), and it returns an array of elements that define all of the given classnames.

Usage

Given the following HTML fragment:

<section>
 <p id="p1" class="aaa bbb"></p>
 <div id="p2" class="aaa"></div>
 <p id="p3" class="bbb ccc"></p>
</section>

A call to document.getElementsByClassName("aaa") would return a NodeList with the two elements -- the paragraph p1 and the div p2 -- because both of these elements contain the "aaa" classname in their class attribute. (Remember, the `class` attribute is a space-separated list of classnames.)

A call to document.getElementsByClassName("ccc bbb") would return a NodeList with only one element: p3, because this is the only element that defines both the "ccc" and "bbb" classnames.

A call to document.getElementsByClassName("bbb ccc") would also return a NodeList with only p3, because the order of classnames is not significant.

A call to document.getElementsByClassName(" bbb ccc ") would also return a NodeList with only p3, because leading and trailing spaces are not significant, and multiple spaces are collapsed and treated as a single delimiter.

A call to document.getElementsByClassName("aaa,bbb") would return no nodes, because none of the elements in this example define the "aaa,bbb" class. The comma is not a delimiter; the only valid delimiter is a space.

Browser compatibility

Compatibility table legend

Test IE8 IE7 IE6 FF3 FF2 Saf3 Op9 Chrome
typeOf(document.getElementsByClassName) != 'undefined' N N N Y N Y Y Y

Further reading


Comment by mikesamuel, Dec 06, 2008

Re "the only valid delimiter is a space", according to the current HTML5 draft, valid delimiters are one or more characters in [ \t\n\f\r].

http://www.whatwg.org/specs/web-apps/current-work/#dom-document-getelementsbyclassname says

having obtained the classes by splitting a string on spaces.
where the splitting operation is defined at http://www.whatwg.org/specs/web-apps/current-work/#split-a-string-on-spaces which says
  1. Collect a sequence of characters that are not space characters.
and http://www.whatwg.org/specs/web-apps/current-work/#space-character says
The space characters, for the purposes of this specification, are U+0020 SPACE, U+0009 CHARACTER TABULATION (tab), U+000A LINE FEED (LF), U+000C FORM FEED (FF), and U+000D CARRIAGE RETURN (CR).


Sign in to add a comment