37

This is my div

<div id="car2" style="display:none;"></div>

Then I have a Show button that will show the div when you click:

$("show").click(function() {
    $("$car2").show();
}); 

So right now I want to check if the div #car2 is still hidden before form submission:

if($('#car2').is(':hidden')) {
    alert('car 2 is hidden');
}

Now here is the problem. Although the div #car2 already show, I still got alert message which means that jQuery assumes the div #car2 is still hidden.

My jQuery version is 1.7.

Thanks.

EDIT:

As jasper said, my code is correct and can be run via this demo.

What I suspect there is some conflict with jQuery form to wizard plugin that I am using with my form. Anyone have any idea to solve this?

2

6 Answers 6

70

You can check the CSS display property:

if ($('#car').css('display') == 'none') {
    alert('Car 2 is hidden');
}

Here is a demo: http://jsfiddle.net/YjP4K/

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

4 Comments

although other answer is correct, but somehow i can only use this method in my form so i will choose this as an answer. thanks.
$('#car').is(:hidden) should work. This also is a cleaner solution than the current one. Check more at api.jquery.com/hidden-selector
@akshay yeah that makes more semantic sense. I think there are edge cases where doing your own check makes sense because there are conditions around the element(s) that screw with the :hidden and :visible selectors.
@akshay you forgot the quotes on :hidden. It should be $('#car').is(':hidden')
31

Try:

if(!$('#car2').is(':visible'))
{  
    alert('car 2 is hidden');       
}

Comments

11

Try

if($('#car2').is(':hidden'))
{  
    alert('car 2 is hidden');       
}

Comments

4

Try checking for the :visible property instead.

if($('#car2').not(':visible'))
{
    alert('car 2 is hidden');       
}

Comments

2

Did you notice your typo, $car2 instead of #car2 ?

Anyway, :hidden seems to be working as expected, try it here.

Comments

1

You can use,

if (!$("#car-2").is(':visible'))
{
      alert('car 2 is hidden');
}

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.