Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check div is hidden using jQuery

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?

like image 275
cyberfly Avatar asked Dec 13 '11 04:12

cyberfly


People also ask

How do you check whether a div is hidden using jQuery?

Answer: Use the jQuery :visible Selector You can use the jQuery :visible selector to check whether an element is visible in the layout or not. This selector will also select the elements with visibility: hidden; or opacity: 0; , because they preserve space in the layout even they are not visible to the eye.

Is Div visible jQuery?

You can use .is(':visible') selects all elements that are visible.

Is visible in jQuery?

The :visible selector in jQuery is used to select every element which is currently visible. It works upon the visible elements. The elements that are consuming space in the document are considered visible elements. The height and width of visible elements are larger than 0.

How do you make a Div visible in jQuery?

To toggle a div visibility in jQuery, use the toggle() method. It checks the div element for visibility i.e. the show() method if div is hidden. And hide() id the div element is visible. This eventually creates a toggle effect.


1 Answers

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/

like image 165
Jasper Avatar answered Sep 28 '22 09:09

Jasper