Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery hide only if visible, show only if hidden [closed]

Tags:

jquery

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
like image 893
jmenezes Avatar asked Nov 13 '13 13:11

jmenezes


People also ask

Is jQuery hide () is equivalent to?

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.

How do I toggle show and hide in jQuery?

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.

How do you check hide or show in jQuery?

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!

Which jQuery filter can be used to check if element is visible?

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.


1 Answers

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();
});
like image 51
tymeJV Avatar answered Nov 07 '22 16:11

tymeJV