I want to make the click function dynamic so as not to repeat code, but it doesn't work for me this way.
for (i = 0; i <= 11 ; i++) {
$('#toggler_'+i).click(function () {
$('#container-serv_'+i).slideToggle(300, function () {
});
}); }
On the other hand, if I do the same thing individually, if it works for me, could someone tell me what the difference is?
$('#toggler_0').click(function () {
$('#container-serv_0').slideToggle(300, function () {
});
});
you can also do something like this:
this would add the function to the click event of all elements whose id starts with 'toggler_'. Inside I am taking the id from the value of the this element attribute, so you can use it in the container-serv_ (since we don't know what your html is like, this could perhaps be simplified).
Also, would you take off the for...
At the end I have added a data-num parameter and I collect it with the this, the code looks like this.