user2225970
user2225970

Reputation:

How to toggle only panel next to flip class

I have problem with javascript basics, how to toggle only panel next to flip class? Not all by one click but every panel separately.

same code on editor: http://jsfiddle.net/GaXPG/

<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
<script> 
$(document).ready(function(){
  $(".flip").click(function(){
    $(".panel").slideToggle("slow");
  });
});
</script>

<style type="text/css"> 
.panel,.flip
{
padding:5px;
text-align:center;
background-color:#e5eecc;
border:solid 1px #c3c3c3;
}
.panel
{
padding:50px;
display:none;
}
</style>
</head>
<body>

<div class="flip">Click to slide the panel down or up</div>
<div class="panel">Hello world!</div>
<div class="flip">Click to slide the panel down or up</div>
<div class="panel">Hello world!</div>
<div class="flip">Click to slide the panel down or up</div>
<div class="panel">Hello world!</div>
<div class="flip">Click to slide the panel down or up</div>
<div class="panel">Hello world!</div>

</body>
</html>

Upvotes: 0

Views: 545

Answers (1)

PSL
PSL

Reputation: 123739

You can use .next() in your case to select the div panel next to the clicked section.

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

If you want to slideUp other panels and toggle the panel respective to the current clicked section. This will help too.

  $('.panel') //Get all panels
       .not($(this).next(".panel") //But not the one next to this
       .slideToggle("slow")) //Toggle this panel
       .slideUp("slow"); //using chaining still get back the remaining panels to slideUp

Fiddle

See .not()

Upvotes: 6

Related Questions