Peter
Peter

Reputation: 549

Jquery - How to check if classname exists

i want to check the style Tag if a classname exist or not.

if ($("head > style:eq(2)").hasClass('className')) 
{
  alert('yes');
}

Upvotes: 3

Views: 2360

Answers (2)

Purefan
Purefan

Reputation: 1506

Another version, same thing, only difference is that sometimes for me selectorText is not defined

var rules = document.styleSheets[0].rules || document.styleSheets[0].cssRules;
for (var i in rules) 
{
    if (typeof rules[i]['selectorText'] != 'undefined' && rules[i]['selectorText'].indexOf("fbconnect_button") >= 0) 
    {
        alert('found!!');
    }
}

Upvotes: 0

Haim Evgi
Haim Evgi

Reputation: 125674

You could access the document.styleSheets object:

see the all way in this answer

get CSS rule's percentage value in jQuery

<script>
    var rules = document.styleSheets[0].rules || document.styleSheets[0].cssRules;
    for (var i=0; rules.length; i++) {
        var rule = rules[i];
        if (rule.selectorText.toLowerCase() == ".classname") {
            alert('found!!');
        }
    }
</script>

Upvotes: 6

Related Questions