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?
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.
You can use .is(':visible') selects all elements that are visible.
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.
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.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With