How do I check if an element is hidden in jQuery?


It is possible to toggle the visibility of an element, using the functions .hide(), .show() or .toggle().



How would you test if an element is visible or hidden?



Since the question refers to a single element, this code might be more suitable:



Same as twernt's suggestion, but applied to a single element; and it matches the algorithm recommended in the jQuery FAQ



You can use the hidden selector:



And the visible selector:



Functions don't work with the visibility attribute.



None of these answers address what I understand to be the question, which is what I was searching for, "How do I handle items that have visibility: hidden?". Neither :visible nor :hidden will handle this, as they are both looking for display per the documentation. As far as I could determine, there is no selector to handle CSS visibility. Here is how I resolved it (standard jQuery selectors, there may be a more condensed syntax):



From How do I determine the state of a toggled element?



You can determine whether an element is collapsed or not by using the :visible and :hidden selectors.



If you're simply acting on an element based on its visibility, you can just include :visible or :hidden in the selector expression. For example:



Often when checking if something is visible or not, you are going to go right ahead immediately and do something else with it. jQuery chaining makes this easy.



So if you have a selector and you want to perform some action on it only if is visible or hidden, you can use filter(":visible") or filter(":hidden") followed by chaining it with the action you want to take.



So instead of an if statement, like this:



Or more efficient, but even uglier:



You can do it all in one line:



The :visible selector according to the jQuery documentation:



Elements with visibility: hidden or opacity: 0 are considered to be visible, since they still consume space in the layout.



This is useful in some cases and useless in others, because if you want to check if the element is visible (display != none), ignoring the parents visibility, you will find that doing .css("display") == 'none' is not only faster, but will also return the visibility check correctly.



If you want to check visibility instead of display, you should use: .css("visibility") == "hidden".



Also take into consideration the additional jQuery notes:



Because :visible is a jQuery extension and not part of the CSS specification, queries using :visible cannot take advantage of the performance boost provided by the native DOM querySelectorAll() method. To achieve the best performance when using :visible to select elements, first select the elements using a pure CSS selector, then use .filter(":visible").



Also, if you are concerned about performance, you should check Now you see me… show/hide performance (2010-05-04). And use other methods to show and hide elements.



This works for me, and I am using show() and hide() to make my div hidden/visible:



How element visibility and jQuery works;



An element could be hidden with display:none, visibility:hidden or opacity:0. The difference between those methods:



opacity:0 hides the element as "visibility:hidden", and it still takes up space in the layout; the only difference is that opacity lets one to make an element partly transparent;



Useful jQuery toggle methods:



I would use CSS class .hide { display: none!important; }.



For hiding/showing, I call .addClass("hide")/.removeClass("hide"). For checking visibility, I use .hasClass("hide").



It's a simple and clear way to check/hide/show elements, if you don't plan to use .toggle() or .animate() methods.



You can also do this using plain JavaScript:



Notes:



Works everywhere



Works for nested elements



Works for CSS and inline styles



Doesn't require a framework



One can simply use the hidden or visible attribute, like:



Or you can simplify the same with is as follows.



Another answer you should put into consideration is if you are hiding an element, you should use jQuery, but instead of actually hiding it, you remove the whole element, but you copy its HTML content and the tag itself into a jQuery variable, and then all you need to do is test if there is such a tag on the screen, using the normal if (!$('#thetagname').length).



ebdiv should be set to style="display:none;". It is works for show and hide:





Source:



Blogger Plug n Play - jQuery Tools and Widgets: How to See if Element is hidden or Visible Using jQuery



jsFiddle:



JSFiddle - ipsjolly - k4WWj



When testing an element against :hidden selector in jQuery it should be considered that an absolute positioned element may be recognized as hidden although their child elements are visible.



This seems somewhat counter-intuitive in the first place – though having a closer look at the jQuery documentation gives the relevant information:



Elements can be considered hidden for several reasons: [...] Their width and height are explicitly set to 0. [...]



So this actually makes sense in regards to the box-model and the computed style for the element. Even if width and height are not set explicitly to 0 they may be set implicitly.



Have a look at the following example:





UPDATE FOR JQUERY 3.x:



With jQuery 3 the described behavior will change! Elements will be considered visible if they have any layout boxes, including those of zero width and/or height.



JSFiddle with jQuery 3.0.0-alpha1:



http://jsfiddle.net/pM2q3/7/



The same JS will then have this output:



This may work:



To check if it is not visible I use !:



Or the following is also the sam, saving the jQuery selector in a variable to have better performance when you need it multiple times:



Example:





Using classes designated for "hiding" elements is easy and also one of the most efficient methods. Toggling a class 'hidden' with a Display style of 'none' will perform faster than editing that style directly. I explained some of this pretty thoroughly in Stack Overflow question Turning two elements visible/hidden in the same div.



Here is a truly enlightening video of a Google Tech Talk by Google front-end engineer Nicholas Zakas:



Example of using the visible check for adblocker is activated:





"ablockercheck" is a ID which adblocker blocks. So checking it if it is visible you are able to detect if adblocker is turned On.



After all, none of examples suits me, so I wrote my own.



Tests (no support of Internet Explorer filter:alpha):



a) Check if the document is not hidden



b) Check if an element has zero width / height / opacity or display:none / visibility:hidden in inline styles



c) Check if the center (also because it is faster than testing every pixel / corner) of element is not hidden by other element (and all ancestors, example: overflow:hidden / scroll / one element over enother) or screen edges



d) Check if an element has zero width / height / opacity or display:none / visibility:hidden in computed styles (among all ancestors)



Tested on



Android 4.4 (Native browser/Chrome/Firefox), Firefox (Windows/Mac), Chrome (Windows/Mac), Opera (Windows Presto/Mac Webkit), Internet Explorer (Internet Explorer 5-11 document modes + Internet Explorer 8 on a virtual machine), Safari (Windows/Mac/iOS)



How to use:



You need to check both... Display as well as visibility:



If we check for $(this).is(":visible"), jQuery checks for both the things automatically.



Maybe you can do something like this





Because Elements with visibility: hidden or opacity: 0 are considered visible, since they still consume space in the layout (as described for jQuery :visible Selector) - we can check if element is really visible in this way:



But what if the element's CSS is like the following?



So this answer to Stack Overflow question How to check if an element is off-screen should also be considered.



A function can be created in order to check for visibility/display attributes in order to gauge whether the element is shown in the UI or not.



Working Fiddle



Simply check visibility by checking for a boolean value, like:



I used this code for each function. Otherwise you can use is(':visible') for checking the visibility of an element.



Also here's a ternary conditional expression to check the state of the element and then to toggle it:




Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).


Would you like to answer one of these unanswered questions instead?

Popular posts from this blog

The Dalles, Oregon

眉山市

清晰法令