Some text

List of all unique class names starting with a prefix

Ok, let's take the 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 on the page starting with _, and send them to another page that will return me with a file that contains the generated CSS style based on these class name.

Now, how to collect all the names of unique classes that begin with the symbol "_" or another prefix? The list may be an array or json. But I prefer json.

+3
source share
2 answers

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

It handles several classes correctly, 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);
+3

+1

All Articles