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.

Recommended Answers

All 4 Replies

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.

You mean to do this:

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

});

It still doesn't work.

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
  });
});

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.