2

Hi I have been having this problem with a slide effect of jQuery, the effect I want to get is as you click on the question the answer slides down. It is working for the first one, but isn't for the next few questions. Here is a link to the example http://jsfiddle.net/p8CLN/3/ $(document).ready(function(){ $("#question").click(function(){ $("#answer").slideToggle("slow"); }); }); I have really been stumped on this one.

user1848831
  • 57
  • 1
  • 6

4 Answers4

2

Convert all your IDs to classes, and give the click function a context:

$(document).ready(function(){
    $(".question").click(function(){
        $(this).next(".answer").slideToggle("slow");
    });
});

See updated fiddle here - http://jsfiddle.net/teddyrised/p8CLN/8/

Terry
  • 63,248
  • 15
  • 96
  • 118
0

IDs MUST be unique.

Here is an updated Fiddle. I have replaced the IDs with a class name and used .next() to get the appropriate answer element.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
0

It is because you are using multiple elements with id's of question and answer. Id's MUST be unique as jquery will stop building a collection after the first id it finds.

I have updated your fiddle to fix:

http://jsfiddle.net/p8CLN/6/

Note I also needed to change CSS and added a use next('.answer') to target the toggle only to the subsequent answer to the clicked question.

        $(document).ready(function(){
          $(".question").click(function(){
            $(this).next(".answer").slideToggle("slow");
          });
        });
Mike Brant
  • 70,514
  • 10
  • 99
  • 103
0

you provide same id for the multiple elements. the ids must be unique. once the document is ready. on document.ready you can bind a click event you can use the next as a solution. here is the example

$(document).ready(function()
{
$(".question").click(function()
{
$(this).next(".answer).slideToggle("slow");
});
});