Mircea
Mircea

Reputation: 11613

jQuery check if element has css attribute

I need to know when I click on an element if this element has a CSS attribute. I am thinking of something like this, but it does not work:

if ($('#element').attr("text-shadow")) {
    alert ('i Have')
}
else {
    alert ('i dont')
}

Any tips on this one? Thanx

Upvotes: 19

Views: 40032

Answers (5)

Khanh Huynh
Khanh Huynh

Reputation: 11

How about this

if ($('#element')[0].style.text-shadow != '') {
    alert ('i Have')
}
else {
    alert ('i dont')
}

Upvotes: 1

yuliskov
yuliskov

Reputation: 1498

In case you want to test 'width' style property. Behavior in Firefox is slightly differ from other browsers.

if (jQuery(value).css('width') == '0px') { // width not set
    jQuery(value).css('width', '320px');
}

Upvotes: 3

Jimmy Baker
Jimmy Baker

Reputation: 3255

How about this instead:

if($('#element').css('text-shadow') == null) ...

Upvotes: 2

Stefan Kendall
Stefan Kendall

Reputation: 67822

if( $('#element').css('text-shadow') != null )  { 
    /*success*/ 
} 
else { 
    /*does not have*/ 
}

Upvotes: 29

prendio2
prendio2

Reputation: 1885

$('#element').css("text-shadow")

Upvotes: 1

Related Questions