Search code examples
javascriptyuiyui2

How to do multiple selectors in YUI


How can i do multiples selectors in yui (yui 2) like in jquery :

$('h1, h2, el1, el2, .content, .title').css('color', 'red');

How can this one be written in yui (without doing YAHOO.util.Dom.addClass on each element seperately)


Solution

  • Some of the DOM methods of YUI accept an array of elements to act on, and the addStlye() method is one of them, so you should be able to do:

    YAHOO.util.Dom.setStyle(['el1', 'el2'], 'color', 'red');
    

    Think it only works with ids though, so the first element should have an id of el1, etc...

    EDIT:

    You can also use the YAHOO.util.Selector module to query the DOM and return the array of elements to pass to setStyle(), e.g:

    var els = YAHOO.util.Selector.query('h1, h2, h3, .some-element');
    
    YAHOO.util.Dom.setStyle(els, 'color', 'red');