0 web3 6 Years Ago I have a problem with my for loop. This is the code: for(i = 1; i <= 30; i++){ var opacity = $("#h"+i).css("opacity"); if(opacity < 1){ $("#h"+i).click(function() { $("#h"+i).css({ opacity: 1 }); }); } } When I put a number like $("#h1") instead of $("#h"+i) It works but only for that object but then I don't have a use for for loop. javascript
0 pritaeas 1,949 6 Years Ago It would be easier to use one class for all those div's. The problem is that i is not defined in the click function, so it loses it's meaning. Not quite sure how to explain it.
0 OPDiscussion Starter web3 6 Years Ago You mean to do this: $("#h"+i).click(function(i) { }); It still doesn't work.
1 Featured Reply pritaeas 1,949 6 Years Ago No. Not what I meant. You have several div's with id="h1" . If you add class="myclick" then you can use: $(".myclick").click(function() { $(this).css({ opacity: 1 }); });
0 OPDiscussion Starter web3 6 Years Ago No. Not what I meant. You have several div's with id="h1" . If you add class="myclick" then you can use: $(".myclick").click(function() { $(this).css({ opacity: 1 }); }); I can't use a class in this. But if I put $("#h"+i).click(function() { $(this).css({ opacity: 1 }); instead of this: $("#h"+i).click(function() { $("#h"+i).css({ opacity: 1 }); It works! Thanks man!