Search code examples
javascriptyuiyui3

Add Class to all table rows with any 2 classes and another style to all table rows with any 3 classes


Another YUI3 question.

I want to add a class(.two) to all table rows with 2 classes and another class(.three) to all table rows with 3 classes.

I found this piece JQuery of code from another question on here, I suspect this will work, but need to convert it to YUI3 and also allow for the two classes to be added, as well as changing the div to somehow:

$(function(){
    var div = $('div[class*=" "]').filter(function(){
        var clsArray = $.trim(this.className.split(' ');
        return clsArray.size > 1;
    });
    div.css('background','yellow');
});

jsfiddle for above exmaple here:http://jsfiddle.net/udBZy/3/

Thanks!

EDIT:

Here is what I have so far, but no luck :(

$(function() {

    YUI().use('node', function(Y) {

        var div = Y.all()('div[class*=" "]').filter(function(){

        var clsArray = Y.all().Lang.trim(this.className).split(' ');

        return clsArray.length > 1;

        });

        div.setStyle('background','yellow');

    });  

});

Solution

  • Here is the solution, via the YUI forums. This might help someone in the future:

    YUI().use('node', function (Y) {
        var re = / {1,}/g;
        var div = Y.all('div[class *= " "]').each(function (n) {
            var numClasses =  Y.Lang.trim(n.get("className")).split(re).length;
    
            n.addClass("numClasses" + numClasses);
            n.setContent(n.getContent() + " found " + numClasses);
        });
    });