title: JQuery Show/Hide All (FAQ page)
body: <p>I'm using two jQuery functions on an FAQ page. The first one activates whenever a question heading (h4) is clicked. It basically slides to show that answer but also makes sure that all other answers are closed (i.e. only 1 answer is open at a time). The second function is one that shows/hides ALL the questions on the page.</p>
  
  <p>My issue arises whenever a user has an FAQ answer open (activated by the first function) and then tries to do a show/hide all. The show/hide function uses a toggling system, so this causes ALL the questions to toggle, including the one that is already showing. The result is that (using say, show all) all questions are shown except the one that was already being shown. That answer is hidden (because it was toggled). Ideally it would stay open since it was already open.</p>
  
  <p>What is the best solution to this problem? The two jQuery functions are as follows:</p>
  
  <pre><code>&lt;script&gt;
  // Clicking a question will show that answer and hide all others
  $(function() {
    $('#faqQuestions h4').each(function() {
      var tis = $(this),
      state = false,
      answerNext = tis.next('div').hide().css('height','auto').slideUp();
      answerAll = $('#faqQuestions').children('div').hide().css('height','auto').slideUp();
      tis.click(function() {
          state = !state;
          answerAll.slideUp(state);
          $('#faqQuestions').children('h4').removeClass('active');
          answerNext.slideToggle(state);
          tis.addClass('active',state);
      });
    });
  });
  &lt;/script&gt;
  
  &lt;script&gt;
  // Show/hide all questions
  var toggle = false;
  $(function() {
    $('a.toggle').click(function(e) {
        var $this = $(this);
        $('#faqQuestions div').toggle(300,function() {
            if(!toggle) {
                $this.text('Hide All Questions/Answers');
                toggle = !toggle;
            }else {
                $this.text('Show All Questions/Answers');
                toggle = !toggle;
            }
         });
        e.preventDefault();
    });
  });
  &lt;/script&gt;
  </code></pre>
  
  <p>For demo purposes, the page I am working on is available here: <a href="http://r-8.us/~richard.r8us/faq" rel="nofollow">http://r-8.us/~richard.r8us/faq</a></p>