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

StackOverflow https://stackoverflow.com/questions/17714377

  •  03-06-2022
  •  | 
  •  

Question

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');

    });  

});
Était-ce utile?

La solution 2

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);
    });
});

Autres conseils

For any JQuery to YUI conversions, you have http://www.jsrosettastone.com/ and no, it is not difficult. Search the page for your JQuery methods and you'll find the equivalents. You will need Y.all() and node.addClass().

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top