I've got something like this, but it doesn't work:
<!-- ... code .. -->
var id = 0;
data.forEach(function(item, i) {
id = id+1;
$( ".button-" + id ).click(function() {
$( ".button-" + id ).toggle();
});
<!-- ... code .. -->
What I want: There are some DIVs like button-1, button-2 ... and so on. I made an toggle and I wanted to automate it with this loop, because the content is dynamically generated. But this seems to be the wrong way... In this loop it does not work.
So how to make toggles dynamically for button-1, button-2, ... ??
It's just about this, which seems to be wrong:
$( ".button-" + id ).click(function() {
$( ".button-" + id ).toggle();
EDIT
Well, maybe my question was a little bit messy Thank you all so far for your answers, I really appreciate it! But there wasn't the right answer for me... So again and now more concrete:
I've got some buttons with material icons, which are supposed to change their state on click.
<a href="" class="likebutton-1"><i class="material-icons">favorite</i></a>
<a href="" class="likebutton-1" style="display:none;"><i class="material-icons">favorite_border</i></a>
The HTML content is generated dynamically, so there's a likebutton-1, likebutton-2...
Then there's another DIV which shows a number of likes:
<div class="likes-1">99 likes</div>
<div class="likes-1" style="display:none;">100 likes</div>
The DIVs a both within a outer container.
And here is my jQuery Code for that:
$( ".likebutton-1" ).click(function() {
$( ".likebutton-1" ).toggle();
$( ".likes-1" ).toggle();
});
Well, that code works for me. The icon toggles from favorite to favorite_border and the likes toggle from 99 to 100... (It's just a demo application, so it's not necessary to really count the likes)
But I want to generate that jQuery code dynamically, too, and that's what is not working... for now I have to set manually toggles for likebutton-1, likebutton-2 ...