YUI Library Home

YUI Library Examples: Dom Collection: Using getElementsByClassName

Dom Collection: Using getElementsByClassName

Clicking the document will use Dom's getElementsByClassName method to collect all of the elements with the class bar applied.

div class="bar"
div class="bar-baz"
div class="bar "
div class=" bar "
div class=" bar baz"
div class=" bar2 baz"
div class="foo"
div class="foo" id="bar"
div class="foo bar baz"

p class="bar"

Using getElementsByClassName

getElementsByClassName, part of the YUI Dom Collection, makes it easy to collect elements that have a given className applied.

To illustrate the use of getElementsByClassName, we'll create a number of <div>s with various combinations of classNames applied. When the document is clicked, we will collect all of the elements that have the className bar.

Add with some markup for the demo elements, including a button to trigger the demo:

1<div class="bar">div class="bar"</div
2<div class="bar-baz">div class="bar-baz"</div
3<div class="bar ">div class="bar "</div
4<div class=" bar ">div class=" bar "</div
5<div class="bar baz">div class=" bar baz"</div
6<div class="bar2 baz">div class=" bar2 baz"</div
7<div class="foo">div class="foo"</div
8<div class="foo" id="bar">div class="foo" id="bar"</div
9<div class="foo bar baz">div class="foo bar baz"</div
10<class="bar">p class="bar"</p
11<button id="demo-run">run</button> 
view plain | print | ?

Now we will define the function that collects the elements with the className of bar present. The first argument of the getElementsByClassName method is the className we are searching for. The second argument is an optional tagName, which will make the search much quicker. The third argument is an optional ID of an HTMLElement, or an actual HTMLElement object to use as the root node to start from.

1<script type="text/javascript"
2    var getByClass = function() { 
3        alert('found: ' + YAHOO.util.Dom.getElementsByClassName('bar''div').length + ' elements'); 
4    }; 
5</script> 
view plain | print | ?

To trigger the demo, we will use the YUI Event Utility's on method to listen for clicks on the button.

1<script type="text/javascript"
2    YAHOO.util.Event.on('demo-run''click', getByClass); 
3</script> 
view plain | print | ?

This is a simple example of how to use the YAHOO.util.Dom.getElementsByClassName method. Keep in mind that the optional arguments should be used whenever possible to maximize the performance of the search.

Configuration for This Example

You can load the necessary JavaScript and CSS for this example from Yahoo's servers. Click here to load the YUI Dependency Configurator with all of this example's dependencies preconfigured.

YUI Logger Output:

Logger Console

INFO 70ms (+3) 8:28:21 PM:

Dom

isAncestor([object HTMLHtmlElement],[object HTMLSpanElement] returning true

INFO 67ms (+28) 8:28:21 PM:

Dom

getElementsBy returning

INFO 39ms (+0) 8:28:21 PM:

Dom

generateId generating yui-log-filter-Dom010

WARN 39ms (+0) 8:28:21 PM:

Dom

batch called with invalid arguments

INFO 39ms (+0) 8:28:21 PM:

Dom

generateId generating yui-log-filter-LogReader09

WARN 39ms (+1) 8:28:21 PM:

Dom

batch called with invalid arguments

INFO 38ms (+0) 8:28:21 PM:

Dom

generateId generating yui-log-filter-Get08

WARN 38ms (+0) 8:28:21 PM:

Dom

batch called with invalid arguments

INFO 38ms (+0) 8:28:21 PM:

Dom

generateId generating yui-log-filter-example07

WARN 38ms (+0) 8:28:21 PM:

Dom

batch called with invalid arguments

INFO 38ms (+0) 8:28:21 PM:

Dom

generateId generating yui-log-filter-global06

WARN 38ms (+0) 8:28:21 PM:

Dom

batch called with invalid arguments

INFO 38ms (+0) 8:28:21 PM:

Dom

generateId generating yui-log-filter-window05

WARN 38ms (+0) 8:28:21 PM:

Dom

batch called with invalid arguments

INFO 38ms (+0) 8:28:21 PM:

Dom

generateId generating yui-log-filter-time04

WARN 38ms (+0) 8:28:21 PM:

Dom

batch called with invalid arguments

INFO 38ms (+0) 8:28:21 PM:

Dom

generateId generating yui-log-filter-error03

WARN 38ms (+0) 8:28:21 PM:

Dom

batch called with invalid arguments

INFO 38ms (+0) 8:28:21 PM:

Dom

generateId generating yui-log-filter-warn02

WARN 38ms (+0) 8:28:21 PM:

Dom

batch called with invalid arguments

INFO 38ms (+0) 8:28:21 PM:

Dom

generateId generating yui-log-filter-info01

WARN 38ms (+1) 8:28:21 PM:

Dom

batch called with invalid arguments

INFO 37ms (+1) 8:28:21 PM:

Dom

generateId generating yui-log-hd00

INFO 36ms (+9) 8:28:21 PM:

LogReader instance0

LogReader initialized

INFO 27ms (+1) 8:28:21 PM:

Get

Appending node: ../../../2.x/build/event-mouseenter/event-mouseenter-min.js

INFO 26ms (+0) 8:28:21 PM:

Get

attempting to load ../../../2.x/build/event-mouseenter/event-mouseenter-min.js

INFO 26ms (+24) 8:28:21 PM:

Get

_next: q0, loaded: undefined

INFO 2ms (+2) 8:28:21 PM:

example

The example has finished loading; as you interact with it, you'll see log messages appearing here.

INFO 0ms (+0) 8:28:21 PM:

global

Logger initialized

Note: You are viewing this example in debug mode with logging enabled. This can significantly slow performance.

Reload with logging
and debugging disabled.

More Dom Collection Resources:

Copyright © 2011 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings