YUI Library JavaScript DHTML





    
Using getElementsByClassName

/*margin and padding on body element
  can introduce errors in determining
  element position and are not recommended;
  we turn them off as a foundation for YUI
  CSS treatments. */
body {
  margin:0;
  padding:0;
}





#foo {
    margin-bottom:1em;
}




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"


run

(function() {
    var getByClass = function(e) {
        alert('found: ' + YAHOO.util.Dom.getElementsByClassName('bar', 'div').length + ' elements');
    };
    YAHOO.util.Event.on('demo-run', 'click', getByClass);
    YAHOO.log("The example has finished loading; as you interact with it, you'll see log messages appearing here.", "info", "example");
})();





   
  
yui_2.7.0b.zip( 4,431 k)