itzikb itzikb - 6 months ago 17
Javascript Question

onclick event toggle div that is exists more than 1 in different div

I uploaded my code into jsfiddle:

https://jsfiddle.net/r26q9ssu/
I'm trying to toggle the

div.que-answer
when I click on

div.pull-left.circle.sh-answer


$("div.question div.sh-answer").click(function(){
$(this).find("div.que-answer").show();
});


If u can see there is a multiple div called question, I don’t remember how to do that I am a little rusty.

Answer

You should use $(this).parent().next() instead DEMO

$("div.question div.sh-answer").click(function() {
  $(this).parent().next("div.que-answer").toggle();
});

Also if you want to hide siblings and add slide effect you can do it like this DEMO

$("div.question div.sh-answer").click(function() {
  $(this).parent().next("div.que-answer").slideToggle();
  $("div.que-answer").not($(this).parent().next("div.que-answer")).slideUp();
});