0

I have a couple of jQuery function with a similar syntax.

$("#item-1").hover(function(){
    $(".item-1").animate({opacity:1},"slow");
},function(){
    $(".item-1").animate({opacity:0},"slow");
});

$("#item-2").hover(function(){
    $(".item-2").animate({opacity:1},"slow");
},function(){
    $(".item-2").animate({opacity:0},"slow");
});

$("#item-3").hover(function(){
    $(".item-3").animate({opacity:1},"slow");
},function(){
    $(".item-3").animate({opacity:0},"slow");
});

My question is how to shorten my code with the help of a loop. I tried the following but that didn’t work:

for (i = 1; i <= 3; ++i) {

  $("#item-" + i).hover(function(){
    $(".item-" + i).animate({opacity:1},"slow");
  },function(){
    $(".item-" + i).animate({opacity:0},"slow");
  });

}
4
  • Your loop should work properly... Commented Jun 25, 2015 at 13:54
  • Maybe you could show the HTML ? Commented Jun 25, 2015 at 13:54
  • Not like this. Besides the approach, his mistake is using a class selector inside. Commented Jun 25, 2015 at 13:55
  • ID and class names are correct as I don’t want to animate the element I hover but another one with the corresponding class name (it’s coincidence that they both have the same name, although maybe a bit misleading in my example …) Commented Jun 25, 2015 at 13:59

6 Answers 6

5

You could you jQuery's attributeStartsWith selector

Description: Selects elements that have the specified attribute with a value beginning exactly with a given string.

The following will work if you have .item-x classes elements:

$('[id^="item-"]').hover(function(){
    $("."+this.id).animate({opacity:1},"slow");
},function(){
    $("."+this.id).animate({opacity:0},"slow");
});

Otherwise in my suggestion you could do it by :

$('[id^="item-"]').hover(function(){
    $(this).animate({opacity:1},"slow");
},function(){
    $(this).animate({opacity:0},"slow");
});
Sign up to request clarification or add additional context in comments.

4 Comments

Fix the selector to "#"+this.id and I will give you that upvote. :D
But I've done according to his question otherwise I could do it $(this).animate....
I know, but his question is flawed :)
Oh, if it should trigger another element, than I get it, he is right.
3

Put a universal class item to all elements and then just call the function for all items:

$(".item").hover(function(){
    $(this).animate({opacity:1},"slow");
},function(){
    $(this).animate({opacity:0},"slow");
});

2 Comments

I am not saying its wrong but what if there is no need of class easy solution is to use multiple selector ...
Sure, but it's always a good practice to group your stuff with similar properties.
3

try out might work for you

$("#item-1, #item-2, #item-3").hover(function(){
    $(this).animate({opacity:1},"slow");
},function(){
    $(this).animate({opacity:0},"slow");
});

Comments

1

This should work :

for (i = 1; i <= 3; ++i) {
  (function(index){
    $("#item-" + index).hover(function(){
      $(".item-" + index).animate({opacity:1},"slow");
    },function(){
      $(".item-" + index).animate({opacity:0},"slow");
    });
  })(i);
}

The problem with your loop is that the variable i is not captured by the hover function. With this, the i variable will be captured properly.

Comments

1

add a class to your item, to select them, and do this

$(".item").hover(function(){
    $(this).animate({opacity:1},"slow");
  },function(){
    $(this).animate({opacity:0},"slow");
  });

Comments

1

If your html is similar to below,

<div class="item-class" id="item-1">
    <div class="item-sub-class"></div>
</div>
<div class="item-class" id="item-2">
    <div class="item-sub-class"></div>
</div>
<div class="item-class" id="item-3">
    <div class="item-sub-class"></div>
</div>

Instead of id use class

$(".item-class").hover(function(){
    $(this).find('.item-sub-class').animate({opacity:1},"slow");
},function(){
    $(this).find('.item-sub-class').animate({opacity:0},"slow");
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.