Another way is by taking the class index .flip
clicked and apply to the same class index .panel
.
The advantage of this way over the .next()
is that the element .panel
does not need to be brother to the element .flip
. The .next()
only works with sibling elements that are after the selected element.
For example, if you have the structure below, the .next()
it won’t work anymore:
<div class="flip"><b><u>Expandir/Esconder</u></b></div>
<div>
<div class="panel">painel 1</div>
</div>
Note above that the div.panel
is not the sister of div.flip
.
Getting the index, as I said, works in any case, because each index of the class .flip
is "married" with each class index .panel
.
Just do this:
$(document).ready(function(){
$(".flip").click(function(){
var idx = $(".flip").index(this); // pega o índice
$(".panel").eq(idx).slideToggle("slow");
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="flip"><b><u>Expandir/Esconder</u></b></div>
<div class="panel">painel 1</div>
<div class="flip"><b><u>Expandir/Esconder</u></b></div>
<div class="panel">painel 2</div>
The .index()
me returns the index of .flip
clicked and use .eq()
to take the same class index .panel
. Very simple!
Boy those little peguntas dots gave a UP huh!
– hugocsl
Are you talking about reputation? I also thought it was strange, I remember being 6k, a beautiful day and has 2k more. He must have bugged SE, I was even looking at my reputation tab yesterday to see where this came from
– Costamilam
The answers until last week were worth 5pts, now went to 10pts, there was retroactive, gave +5 to the old questions :D, I took 3.5k in this rss
– hugocsl
@hugocsl I’m totally outdated shusauhusahuhsa
– Costamilam