1

How can I check a value (from the CSS tag) with jQuery?

CSS

#my_div { display:none; }

JS

$(document).ready(function()
{

  if($('#my_div').is(':hidden'))
  {
     $('body').append('HIDDEN');   
  }
  else
  {
     $('body').append('VISIBLE');   
  }

}

This did not work. Hope somebody can help me.

Example: http://jsfiddle.net/9fSM6/

3 Answers 3

3

You are missing the closing parenthesis from ready: );

Also, put a <div id="my_div"></div> in your code, otherwise it will be handled as 'visible', because you have no 'my_div' element, so it is also not hidden.

Updated code: http://jsfiddle.net/ug6Dv/

Re-updated code: http://jsfiddle.net/PcmJW/

Sign up to request clarification or add additional context in comments.

4 Comments

Ah - didn't spot that, that fixes problem.
Hi, this is my problem. This div doesnt exists in my code so i must check the css (direct). Thanks for the fast answer.
I don't know if I unserstood what you mean by 'check the css', but if 'my_div' may not exist in the document, then check it using $('#my_div').length
@Peter new fiddle fork, using length
2

Worked just fine for me when I added such div element: http://jsfiddle.net/9fSM6/3/

When you don't have any element, the is() will always return undefined so the else part will always be triggered.

Comments

2

You just could use css function. It return value of given css property:

$('#my_div').css('display')

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.