Clicking the button will use Dom's
hasClass method to test if the element has the class
To illustrate the use of
hasClass, we'll create a
foo with the className of
baz. When the button is clicked, we will test whether the
baz is present.
Add some markup for the demo element:
Now we will define the function that tests whether
foo has the
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
false, depending on whether the
className exists on the element.
To trigger the demo, we will use the YUI Event Utility's
on method to listen for clicks on the button.
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.
Note: Logging and debugging is currently turned off for this example.
All YUI 2.x users should review the YUI 2.8.2 security bulletin, which discusses a vulnerability present in YUI 2.4.0-2.8.1.