0

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.

2
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by web3
0

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

You mean to do this:

$("#h"+i).click(function(i) {

});

It still doesn't work.

1

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

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!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.