views:

44

answers:

1

I'm trying to build an FAQ page. The page will have 2 columns: first column will have list of questions, second column will have the answers.

Essentially I'd like to hide the answer column and when a question is clicked, it will fade in. When another question is clicked, the old answer will fade out and the new answer fades in. So I guess a toggle/switch effect?

Here is the structure of my html output from Drupal:

<!-- Question column -->

<div id="question>

     <div class="views-row views-row-1">
          <div class="question">Question 1 Here!</div>
     </div>

     <div class="views-row views-row-2">
          <div class="question>Question 2 Here!</div>
     </div>

      <div class="views-row views-row-3">
          <div class="question>Question 3 Here!</div>
     </div>

</div>


<!-- Answer column -->

<div id="answer>

     <div class="views-row views-row-1">
          <div class="answer">Answer 1 Here!</div>
     </div>

     <div class="views-row views-row-2">
          <div class="answer>Answer 2 Here!</div>
     </div>

      <div class="views-row views-row-3">
          <div class="answer>Answer 3 Here!</div>
     </div>

</div>

Now here's what I managed to do so far. I've managed to make the first question toggle on/off itself with an animated fade effect. When it comes to hiding other divs to show a new div, I'm lost. Could you help tweak my jQuery plugin?

      jQuery.fn.fadeToggle = function(speed, easing, callback) {

      return this.animate({opacity: 'toggle'}, speed, easing, callback);  

      };



$(document).ready(function() {

  $("#answer .views-row").hide();

  $("#question .views-row-1").click(function(){

  $("#answer .views-row-1").fadeToggle("slow");

  });

  });

Very much appreciated for any help! Cheers.

A: 

I think this is what you're after:

jQuery.fn.fadeToggle = function(speed, easing, callback) {
    return this.animate({opacity: 'toggle'}, speed, easing, callback);  
};
$(function() {
    $("#answer .views-row").hide();
    $("#question .views-row").click(function(){
        var i = $(this).index();
        $("#answer .views-row").eq(i).fadeToggle("slow").siblings().fadeOut();
    });
});

You can give it a try here, what we're doing here is getting the .index() of the <div> you clicked up top and showing the corresponding <div> at that index below (using .eq()). The .siblings().fadeOut() is just a guess, that hides the previous answer so only one at a time shows, if you want to show any number at once, just remove that portion. ​

Nick Craver
Hi Nick,Thanks for your quick response! Your example does exactly what I want. And with a touch of CSS it would be perfect. But I can't seem to get it working. I think its because the I simplified the html a bit which messes up getting the index of the div. Let me give you a link of the my FAQ page:http://gemini-lights.com/new/supportCheers!
Chris
@Chris - Something in your HTML makes `.index()` go completely wonky, still trying to figure out what, copying the whole of `.content` it works fine: http://jsfiddle.net/nick_craver/Zzy6N/1/
Nick Craver
@Chris - Ah you're using a very old version of jQuery in that page, 1.2.6, any chance of upgrading?, the latest in 1.4.2.
Nick Craver
Will give that a go! I'll update now. After that should I use your first plugin?
Chris
@Chris - Yup, in case upgrading *isn't* an option, here's a version that'll work with 1.2.x and 1.3.x jQuery: http://jsfiddle.net/nick_craver/Zzy6N/2/
Nick Craver
@Nick Thank you so much!! You were right, upgrading jQuery to 1.4.2 wasn't an option in Drupal 6.x. I spent a while figuring that one out! Your help is unbelievable, quick, informative and very thorough!I'd buy you a beer. Many thanks.
Chris
@Chris - Welcome :)
Nick Craver