Jump to content

How to force slideToggle to open or close all...


Go to solution Solved by joetaylor,

Recommended Posts

ok, to set this up.. This is a page with about 50 paragraphs..  I essentially have two scripts on this page.  One to open those paragraphs one by one.  And another to open and close all of them.

 

My problem is when I've opened a one or more paragraphs and decide to then click the link that opens all, ... it actually closes the 'open paragraphs' and opens the rest.   :facepalm:  

 

Is there something I can add to this code that would force all to open or close instead of just toggling each individually?  

// FAQ hide show ALL
$(document).ready(function() {
 $('.nav-toggle').click(function(){

var collapse_content_selector = $(this).attr('href'); 

var toggle_switch = $(this);
$(collapse_content_selector).slideToggle('150', function(){
 if($(this).css('display')=='none'){

toggle_switch.html('[x] expand all');
 }else{
toggle_switch.html('[x] hide all');
 }
});
 });

}); 

 My HTML

<button href=".slideall" class="nav-toggle">[x] expand all</button>

Then of course each paragraph is assigned that class (.slideall)..

 

Thanks for your input in advance!

 

~Joe

Thanks, makes sense.  I was playing around with it a lot today and I noticed that or similar worked.  But I have to figure out how to code it so I can still have the button text toggle back and forth.. and have just the one button/ link.  It's easy peezy to have two buttons.

 

I've been scouring the net for hours and hours today testing a myriad of avenues.. been thinking there's GOT to be a jquery plugin that will do something as simple as this, but I'm not having a lot of luck - found a couple, but they are either too complicated or don't work for some reason.

 

I'm trying to code it myself from scratch (or adding to what I have there) thinking that I would use an if/then type statement..  

 

I would assume that would work.  

 

Learning a lot, but still not there..  :blink:

Use a class to indicate the state of the toggle. Based on whether that class exists or not you can determine whether you want to slideUp or slideDown.

 

var toggle_switch = $(this);
if (toggle_switch.is('.expanded')){
   $(collapse_content_selector).slideUp(150, function(){
      toggle_switch.text('[x] Expand All').removeClass('expanded');
   });
}
else {
   $(collapse_content_selector).slideDown(150, function(){
      toggle_switch.text('[x] Collapse All').addClass('expanded');
   });
}
  • Solution

Wow, thanks a million!  I was hoping there was a simple and elegant way to accomplish this.  

 

I can't wait until 'what my mind envisions' and 'my coding abilities' meet someday.  I had a hunch if statement/then/else probably was the answer combined with manipulating classes, but my brain wasn't connecting the coding dots yet.   

 

unrelated note...

:hail_freaks: I can't wait until I can give back more to this community; you've all been great so far.. I've been lurking in the other forum areas that I have more experience with waiting for an opportunity to help and give back; I'll spend some of the time I would have been figuring this problem out today at phpfreaks! ;D  ty all!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.