When using jQuery, is it possible to hide a div
only if it is not hidden, and show it only if it is not shown, instead of adding the same classes to it again?
Can something like an if be used there?
Eg:
$(document).ready(function() {
$(".trigger").click(function() {
// Hide it but only if not hidden - hide
// Later in the script - Show it but only If it's not visible.
});
});
<div class="user">Example Div</div>
<div class="trigger">Load</div>
I've stripped things down for this question. The real thing is a lot different.
Edit
Toggle is not what I'm trying to do. Toggle will change a class. I'm compulsorily trying to hide it, but only if it's not hidden. That is: add the hidden class only if it's current class is not hidden.
If class != 'hidden' then add the Hidden class
jQuery hide() Method The hide() method hides the selected elements. Tip: This is similar to the CSS property display:none. Note: Hidden elements will not be displayed at all (no longer affects the layout of the page). Tip: To show hidden elements, look at the show() method.
The toggle() method toggles between hide() and show() for the selected elements. This method checks the selected elements for visibility. show() is run if an element is hidden. hide() is run if an element is visible - This creates a toggle effect.
To check if an element is hidden or not, jQuery :hidden selector can be used. .toggle() function is used to toggle the visibility of an element. Click!
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.
Use an instance of this
and toggle
$(".user").click(function() {
// Only if not hidden - hide
// Later in the script - Only If not visible show.
$(this).next(".trigger").toggle();
});
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