views:

51

answers:

1

I'm nearly complete with this, so I have one final bug I need your help figuring out.

I'll start with the issue first...

Demo: http://jsbin.com/ucalu3/7/

Scenario 3 doesn't work. When you click "expand all" and click a question to collapse the answer, it works just fine. The issue is, now click "collapse all" and it will expand all the answers. I'd like it to actually collapse all the answers just as the "collapse all" link says it should instead of it expanding all the answers.

The other 2 scenarios work just fine, which are:

Scenario 1 works. (refresh the page to reset accordion) When you click “expand all”, it will change to “collapse all”. The answers will expand accordingly and the active question state will swap appropriately. When you click “collapse all”, all the questions/answers will return to its default state.

Scenario 2 works. (refresh the page to reset accordion) When you click a question first and then click "expand all", it will expand all the answers and when you click "collapse all" it will collapse all the answers.


Code

$(document).ready(function() 
  { 
      $('.question').click(function() 

        {
          if($(this).next().is(':hidden') != true) 
            {
              $(this).removeClass('active'); 
              $(this).next().slideUp('normal');
            }
            else 
            {
              $('.question').removeClass('active');  
              $('.answer').slideUp('normal');

              if($(this).next().is(':hidden') == true) 
                {
                  $(this).addClass('active');
                  $(this).next().slideDown('normal');
                 }
            }
         });

        $('.answer').hide();

        $('.swap').click(function() 

          {
            if($('.question').next().is(':hidden') != true) 
            {
              $('.answer').slideUp('normal');
              $('.question').removeClass('active'); 
              $(this).text($(this).text() == 'Expand All' ? 'Collapse All' : 'Expand All');
            }
            else
            {
              $('.answer').slideDown('normal');
              $('.question').addClass('active'); 
              $(this).text($(this).text() == 'Expand All' ? 'Collapse All' : 'Expand All');
            }
          }
        );
  });

Thank you for your help!

Sincerely, Evan

+1  A: 

I would check against the current text to eliminate any confusion, by replacing this check:

if($('.question').next().is(':hidden') != true)

With this one:

 if($(this).text() == 'Collapse All') 

You can test it out here.

Nick Craver
@Nick - LOL! I was reading your bio just now when you answered my question. You solved the issue I didn't think of... Thank you Nick. I appreciate your support!
Evan
@Nick - I have a follow up question for you. When the "Expand All" is clicked, and a question is clicked to collapse it, I changed it to expand/collapse only that question as long as "expand all" was clicked preventing accidental collapsing of all questions. As a result of this change, (refresh the page to reset the accordion), if you now click on an initial question and then a second question, your second click doesn't collapse the previous question. How do I have the previous questions in this scenario collapse upon each click?You can demo it here: http://jsbin.com/ucalu3/9/
Evan
@Evan - You can do that and shorten your code a bit using toggle functions, like this: http://jsbin.com/ucalu3/11
Nick Craver
@Nick - Wow! You've shorten considerably! I've observed now that when "Expand All" is clicked and then a question is clicked, it collapses all the questions. It should only collapse that one question, but also, the user should be able to expand that same question again if they click it again preventing all the questions from collapsing if they've come through the "Expand All" scenario.
Evan
@Evan - Can add a check based on your `active` class, like this: http://jsbin.com/ucalu3/12
Nick Craver
@Nick - So, in your link, click "Expand All" > click a question > click the same question > it will collapse all questions, but that 1 question. I'm asking for this process to function like a panel. Now, if you don't click "Expand All" then expand/collapse should function like an accordion. So, I tried to modify what you started by changing $(this).toggleClass('active') to be $(this).addClass('active') and it now does the accordion/panel slide properly but now the question that was clicked doesn't reverse/active arrow when the question is clicked againExample: http://jsbin.com/ucalu3/13/
Evan
@Evan - oops, that `.addClass()` should have been `.toggleClass()` as well: http://jsbin.com/ucalu3/14
Nick Craver
Evan