19

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

RyanS
  • 627
  • 1
  • 10
  • 26
Mircea
  • 11,373
  • 26
  • 64
  • 95

5 Answers5

29
if( $('#element').css('text-shadow') != null )  { 
    /*success*/ 
} 
else { 
    /*does not have*/ 
}
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Stefan Kendall
  • 66,414
  • 68
  • 253
  • 406
  • for some reason this does not work. There must be something wrong with my code. – Mircea Apr 21 '10 at 19:06
  • It works for me with 1.4.2. Which version of jQuery are you using. Which browser? Can you provide a sample .html page and host it somewhere so I can debug the issue and see what's going on? – Stefan Kendall Apr 21 '10 at 19:24
  • @Bears: Maybe, but in this specific instance, the two should be equivalent, and your solution is an extra non-compressable byte :P. – Stefan Kendall Apr 21 '10 at 19:25
  • Sorry it was a browser thing. i restart it and works now. Thanx. – Mircea Apr 21 '10 at 19:30
  • Would this work if I needed to check a certain attribute's current value (example >> left: 0)? – Tyler Lazenby Jun 24 '15 at 22:21
3

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');
}
yuliskov
  • 1,379
  • 15
  • 16
2

How about this instead:

if($('#element').css('text-shadow') == null) ...
Jimmy Baker
  • 3,215
  • 1
  • 24
  • 26
  • Yes, thank you. I opened this question before any answers had been posted then stepped away from my desk for a moment. My apologies. – Jimmy Baker Apr 21 '10 at 19:09
1

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

prendio2
  • 1,885
  • 17
  • 25
1

How about this

if ($('#element')[0].style.text-shadow != '') {
    alert ('i Have')
}
else {
    alert ('i dont')
}
Lucifer
  • 29,392
  • 25
  • 90
  • 143