1

I've checked through the forum and cant seem to find a solution to my problem.

I have an array of id's and I'm iterating through them with a for loop adding mouse events for enter and leave. However the events all bind to the final ID, which is probably a for each issue.

I've put it up on a fiddle below, any help would be appreciated - my brain is a bit frazzled today.

http://jsfiddle.net/shanemccster/e48vu/4/

cards = new Array('#box0','#box1','#box2');
function bindCardEvents(){
    for (var i=0; i<cards.length; i++){
        var targetID = '#'+$(cards[i]).attr('id');
        $(targetID)
            .mouseenter(function() {
                TweenMax.to(targetID,0.3, {opacity:0.5} )
            }).mouseleave(function() {
                TweenMax.to(targetID,0.3, {opacity:1}) 
            }); 
    }
}
bindCardEvents();

Thanks

2
  • I recommend to add some class selector for all this(like a .card), or use [id^=box] selector Commented Apr 1, 2014 at 17:16
  • Your fault is incorrect events understanding(or js closures) When events triggered targetID already overriden by next loop iterations... Commented Apr 1, 2014 at 18:00

2 Answers 2

1

You dont have to iterate. You can use like

   $("[id^=box]")
        .mouseenter(function() {
            TweenMax.to(this,0.3, {opacity:0.5} )
        }).mouseleave(function() {
            TweenMax.to(this,0.3, {opacity:1}) 
        }); 

It will bind the events to all the elements whose id starts with box

Demo

Sign up to request clarification or add additional context in comments.

3 Comments

FYI: you now have an undefined targetID
@vp_arth, I know, I was letting the author know so he can update his answer.
Such a simple thing, I love when I learn something new. Thank you!
0

jsFiddle Demo

You can just join the ids together with , which by convention adds the selectors to a set in jQuery. After doing that, you can assign the event to the set returned by those selectors. Each event will have access to the current element this. TweenMax.to expects a selector or an element, so you can pass this into the function without manually constructing an id selector.

var cards = new Array('#box0','#box1','#box2');
var cardSelector = cards.join(",");
function bindCardEvents(){
  $(cardSelector).mouseenter(function() {
    TweenMax.to(this,0.3, {opacity:0.5} )
  }).mouseleave(function() {
    TweenMax.to(this,0.3, {opacity:1}) 
  });
}
bindCardEvents();

For brevity this will also work

$(cards.join(",")).mouseenter(function() {
    TweenMax.to(this,0.3, {opacity:0.5} )
  }).mouseleave(function() {
    TweenMax.to(this,0.3, {opacity:1}) 
});

2 Comments

@ShaneMcCarthy - It is definitely less efficient to check if every element on the page has an id which starts with box. If you want to save time, just use $(cards.join(',')).mouseenter ... which trades 3 characters for a far higher efficient selection.
Ah yes, I didn't think of that. Good point, looks like I'll be sticking with your example. Thanks for your help Travis.

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.