1
Can anyone explain why my code doesn’t work? I don’t know exactly how many divs
will generate, but the maximum is 25.
So I wanted each click button to specifically open the div
"glue" attached to it.
In my mind, I put a for
that goes along, naming the class --- if i
is 2nd the click_a2
will open the cola2
and so on.
jQuery and HTML:
for (i = 0; i < 25; i++) {
$('.click_a'+i).click(function () {
$('.cola'+i).slideToggle();
});
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="click_a1"> Detalhes </button>
<div class="cola1" style="display:none" >
hey!
</div>
Note: Understand that I will put a variable (instead of the 1
) that will add up everything that code runs.
Thank you very much, I will have to study a little more to understand why this happens. I thought the variable would continue the same value since it does not leave the for. Anyway, thank you!
– dHEKU