Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making slideToggle() elements collapsed by default in jQuery

Tags:

jquery

I have a set of divs that I want to make collapsible/expandable using jQuery's slideToggle() method. How do I make all of these divs collapsed by default? I'd like to avoid explicitly calling slideToggle() on each element during/after page rendering.

like image 567
Craig Walker Avatar asked Dec 01 '08 23:12

Craig Walker


People also ask

How to use jQuery slideToggle?

The jQuery slideToggle() method toggles between the slideDown() and slideUp() methods. If the elements have been slid down, slideToggle() will slide them up. If the elements have been slid up, slideToggle() will slide them down.

What does slideToggle do?

The . 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.

Which jQuery function is used to display or hide the matched elements with a sliding motion?

slideToggle() Display or hide the matched elements with a sliding motion.

What is slide up and slide down in jQuery?

jQuery slideUp() MethodThe slideUp() method slides-up (hides) the selected elements. Note: Hidden elements will not be displayed at all (no longer affects the layout of the page). Tip: To slide-down (show) elements, look at the slideDown() method.


1 Answers

You will have to assign a style: display:none to these layers, so that they are not displayed before javascript rendering. Then you can call slideToggle() without problems. Example:

<style type="text/css">     .text{display:none} </style>  <script type="text/javascript">     $(document).ready(function() {             $('span.more').click(function() {             $('p:eq(0)').slideToggle();             $(this).hide();         });     }); </script>  <body>                     <p class="text">           I am using jquery <br/>           I am using jquery <br/>           I am using jquery <br/>           I am using jquery <br/>           I am using jquery <br/>           I am using jquery <br/>                  </p>       <span class="more">show</span> 
like image 106
netadictos Avatar answered Oct 10 '22 14:10

netadictos