YUI recommends YUI 3.

YUI 2 has been deprecated since 2011. This site acts as an archive for files and documentation.

YUI Library Home

YUI Library Examples: Dom Collection: Using hasClass

Dom Collection: Using hasClass

Clicking the button will use Dom's hasClass method to test if the element has the class baz applied.

foo

Using hasClass

hasClass, part of the YUI Dom Collection, makes it easy to test whether a given className is present on an element.

To illustrate the use of hasClass, we'll create a <div> called foo with the className of bar and baz. When the button is clicked, we will test whether the className baz is present.

Add some markup for the demo element:

1<div id="foo" class="bar baz">foo</div> 
2<button id="demo-run">run</button> 
view plain | print | ?

Now we will define the function that tests whether foo has the className baz applied. The first argument of the hasClass method is either the ID of an HTMLElement, or an actual HTMLElement object. The second is the className we are testing for. The hasClass method returns true or false, depending on whether the className exists on the element.

1<script type="text/javascript"
2    var testClass = function() { 
3        alert(YAHOO.util.Dom.hasClass('foo''baz')); 
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', testClass); 
3</script> 
view plain | print | ?

This is a simple example of how to use the YAHOO.util.Dom.hasClass method. One of the benefits of this method is that it works regardless of how many classNames are present in the class attribute.

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 63ms (+0) 3:54:15 PM:

Dom

generateId generating yui-log-filter-Dom010

WARN 63ms (+0) 3:54:15 PM:

Dom

batch called with invalid arguments

INFO 63ms (+0) 3:54:15 PM:

Dom

generateId generating yui-log-filter-LogReader09

WARN 63ms (+0) 3:54:15 PM:

Dom

batch called with invalid arguments

INFO 63ms (+0) 3:54:15 PM:

Dom

generateId generating yui-log-filter-Get08

WARN 63ms (+0) 3:54:15 PM:

Dom

batch called with invalid arguments

INFO 63ms (+0) 3:54:15 PM:

Dom

generateId generating yui-log-filter-example07

WARN 63ms (+0) 3:54:15 PM:

Dom

batch called with invalid arguments

INFO 63ms (+0) 3:54:15 PM:

Dom

generateId generating yui-log-filter-global06

WARN 63ms (+0) 3:54:15 PM:

Dom

batch called with invalid arguments

INFO 63ms (+0) 3:54:15 PM:

Dom

generateId generating yui-log-filter-window05

WARN 63ms (+0) 3:54:15 PM:

Dom

batch called with invalid arguments

INFO 63ms (+0) 3:54:15 PM:

Dom

generateId generating yui-log-filter-time04

WARN 63ms (+0) 3:54:15 PM:

Dom

batch called with invalid arguments

INFO 63ms (+0) 3:54:15 PM:

Dom

generateId generating yui-log-filter-error03

WARN 63ms (+0) 3:54:15 PM:

Dom

batch called with invalid arguments

INFO 63ms (+0) 3:54:15 PM:

Dom

generateId generating yui-log-filter-warn02

WARN 63ms (+1) 3:54:15 PM:

Dom

batch called with invalid arguments

INFO 62ms (+0) 3:54:15 PM:

Dom

generateId generating yui-log-filter-info01

WARN 62ms (+0) 3:54:15 PM:

Dom

batch called with invalid arguments

INFO 62ms (+1) 3:54:15 PM:

Dom

generateId generating yui-log-hd00

INFO 61ms (+36) 3:54:15 PM:

LogReader instance0

LogReader initialized

INFO 25ms (+0) 3:54:15 PM:

Get

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

INFO 25ms (+1) 3:54:15 PM:

Get

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

INFO 24ms (+21) 3:54:15 PM:

Get

_next: q0, loaded: undefined

INFO 3ms (+3) 3:54:15 PM:

example

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

INFO 0ms (+0) 3:54:15 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