Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get list of CSS Classes in StyleSheet using javascript/jQuery [duplicate]

Possible Duplicate:
How can you determine if a css class exists with Javascript?

Is there a way to check if there is a class named 'some-class-name' in css?

For example, I have:

<style type="text/css">
    .box {
        position: absolute;
        left: 150%;
    }
</style>

My intention is to randomly assign a class to a div on certain event:

var classList = []; //Need to populate this array
$('#updateClass').on('click', function() {
    $('#resultDiv').attr('class',classList[Math.floor((Math.random()*classList.length)+1)]);
}); 

Is it possible to check by JS/jQuery whether a class named 'box' exists in the stylesheet?

I referred to This Question - How can you determine if a css class exists with Javascript?, But the answer didn't help me because the link in the answer doesn't exists anymore.

Thanks.

like image 380
Akhil Sekharan Avatar asked Dec 19 '12 07:12

Akhil Sekharan


1 Answers

As mentioned above (How can you determine if a css class exists with Javascript?), you could scan the complete CSS for the requested class, or you could add the requested class to an element and check if the attriubtes of the class apply to it. For example:

document.getElementById('yourTestElementID').className=newClass;
if(document.getElementById('yourTestElementID').style.position === 'thevalue' ) {
  // class exists....
}
like image 177
axel.michel Avatar answered Nov 11 '22 08:11

axel.michel