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

5 Answers 5

29
if( $('#element').css('text-shadow') != null )  { 
    /*success*/ 
} 
else { 
    /*does not have*/ 
}
Sign up to request clarification or add additional context in comments.

5 Comments

for some reason this does not work. There must be something wrong with my code.
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?
@Bears: Maybe, but in this specific instance, the two should be equivalent, and your solution is an extra non-compressable byte :P.
Sorry it was a browser thing. i restart it and works now. Thanx.
Would this work if I needed to check a certain attribute's current value (example >> left: 0)?
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');
}

Comments

2

How about this instead:

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

1 Comment

Yes, thank you. I opened this question before any answers had been posted then stepped away from my desk for a moment. My apologies.
1

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

Comments

1

How about this

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.