In jQuery, it is possible to toggle the visibility of an element. You can use the functions .hide()
, .show()
or .toggle()
.
Using jQuery, how would you test if an element is visible or hidden?
In jQuery, it is possible to toggle the visibility of an element. You can use the functions Using jQuery, how would you test if an element is visible or hidden? |
||||
As, the question refers to a single element, this code might be more suitable:
Same as twernt's suggestion, but applied to a single element. |
|||||||||||||||||||||
|
You can use the
And the
|
|||||||||||||||||||||
|
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
|
|||||||||||||||||||||
|
From How do I determine the state of a toggled element? : You can determine whether an element is collapsed or not by using the
If you're simply acting on an element based on its visibility, 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 So instead of an
Or more efficient, but even uglier:
You can do it all in one line:
|
|||||
|
It's worth mentioning (even after all this time), that This can be tested using Tsvetomir Tsonev's helpful test snippet. Just remember to change the version of jQuery, to test under each one. |
||||
The
This is useful in some cases and useless in others, because if you want to check if the element is visible ( If you want to check visibility instead of display, you should use: Also take into consideration the additional jQuery notes:
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. |
||||
|
How element visibility and jQuery works; An element could be hidden with "display:none", "visibility:hidden" or "opacity:0". The difference between those methods:
Useful jQuery toggle methods:
|
|||||
|
This works for me, and I am using
|
||||
|
I would use CSS class For hiding/showing, I call It's a simple and clear way to check/hide/show elements, if you don't plan to use |
|||||||||
|
You can also do this using plain JavaScript:
Notes:
|
|||||||||||||
|
One can simply use the
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 |
|||||||||
|
This may work:
|
|||||||||
|
HTML
jQuery
Source: jsFiddle: |
|||||||||||||
|
Use class toggling, not style editing . . .Using classes designated for "hiding" elements is easy and also one of the most efficient methods. Toggling a class 'hidden' with a JavaScript Best Practices and OptimizationHere is a truly enlightening video of a Google Tech Talk by Google front-end engineer Nicholas Zakas:
|
||||
|
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:
|
|||||||||||||
|
When testing an element against This seems somewhat counter-intuitive in the first place – though having a closer look at the jQuery documentation gives the relevant information:
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: JSFiddle: HTML:
CSS:
JavaScript:
|
||||
|
Also here's a ternary conditional expression to check the state of the element and then to toggle it:
|
|||||
|
|
|||
|
After all, none of examples suits me, so I wrote my own. Tests (no support of Internet Explorer a) Check if the document is not hidden b) Check if an element has zero width / height / opacity or 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: d) Check if an element has zero width / height / opacity or 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 |
||||
|
Because
|
||||
|
|
||||
|
|
|||
|
Thank you for your interest in this question.
Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.
Would you like to answer one of these unanswered questions instead?
.toggle()
was deprecated.toggle([duration] [, complete])
(or with no arguments) is still perfectly valid. – Matt Browne May 27 '13 at 4:53toggle()
method that shows/hides an element (the one being used here) is NOT deprecated. The othertoggle(handler, handler)
method has been deprecated. – L S Jun 2 '13 at 1:04