Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listing all unique class name that begin with a prefix

Well, lets see a markup as an example.

<div class="_round_5">Some text</div>
<div class="_brTop_5">Another Text</div>

My idea is to collect all the unique class name in a page that begin with a _ and post them to a different page, which will return me with a file that contains a generated CSS style based on those class name.

Now, How to collect all the unique class names that begin with a "_" or some other prefix also? The list might be an array or json. But I prefer json.

like image 542
Starx Avatar asked Jan 27 '26 06:01

Starx


1 Answers

Try: http://jsfiddle.net/54kzu/3/

It correctly handles multiple classes, as you requested in the comments.

var uniqueClasses = [];

$('[class]').each(function() {
    var thisClasses = $(this).attr('class').split(/\s+/);
    $.each(thisClasses, function(i, thisClass) {
        if (thisClass.substring(0,1) == '_' && $.inArray(thisClass, uniqueClasses) == -1) {
            uniqueClasses.push(thisClass);
        }
    });
});

console.log(uniqueClasses);
like image 126
thirtydot Avatar answered Jan 28 '26 19:01

thirtydot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!