Last updated on May 7th, 2023 at 12:04 pm
Reading Time: < 1 minuteHow do I toggle the visibility of an element using .hide()
, .show()
, or .toggle()
?
How do I test if an element is visible
or hidden
?
Solution No 1
We use jQuery’s is() to check the selected element with another element, selector or any jQuery object. This method traverses along the DOM elements to find a match, which satisfies the passed parameter. It will return true if there is a match, otherwise return false.
// Checks CSS content for display:[none|block], ignores visibility:[true|false]
$(element).is(":visible");
// The same works with hidden
$(element).is(":hidden");
Solution No 2
You can use the hidden
selector:
// Matches all elements that are hidden
$('element:hidden')
And the visible
selector:
// Matches all elements that are visible
$('element:visible')