Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing true / false to slideToggle()

Tags:

jquery

In jQuery, I know that we can pass true or false value to the function to toggle visibility of an element. I can't get slideToggle to do the same thing. Any help please?

For example, instead of:

if (true)
    $('something').slideDown();
else
    $('something').slideUp();

I would really like to do something similar to:

$('something').toggle(myBoolValue);

But I want to do it with slideToggle.

Look at my example of jsfiddle to know what I am trying to do.

Is that possible?

like image 761
LocustHorde Avatar asked Nov 29 '11 15:11

LocustHorde


People also ask

What does slideToggle do?

slideToggle() method animates the height of the matched elements. This causes lower parts of the page to slide up or down, appearing to reveal or conceal the items. If the element is initially displayed, it will be hidden; if hidden, it will be shown. The display property is saved and restored as needed.

What does slideToggle do in jquery?

The slideToggle() method toggles between slideUp() and slideDown() for the selected elements. This method checks the selected elements for visibility. slideDown() is run if an element is hidden. slideUp() is run if an element is visible - This creates a toggle effect.


1 Answers

slideToggle optionally takes 2 parameters duration and a callback so passing true/false to show/hide will not work. You might have to write your own plugin which will implement this logic. Something like this

$.fn.mySllideToggle = function(show){
   if(show){
      $(this).slideDown();
   }
   else{
      $(this).slideUp();
   }
}
like image 178
ShankarSangoli Avatar answered Oct 23 '22 22:10

ShankarSangoli