0

I found a way to bind single elements in an array with a for loop to event handlers in jQuery.

This guide was useful to push me in that direction:
http://www.foliotek.com/devblog/keep-variable-state-between-event-binding-and-execution/

Now I am one level deeper and I am trying to bind muiltiple elements with the same prefix in an array to event handlers in jQuery.

Here's what works:

var menu=new Array("#power","#services","#cashback","#schedule");

$(document).ready(function(){
    $(function() {
        for(var i in menu)
        {
            (function() {
                var x = menu[i];
                var y = menu[i]+'_menu';
                 $(x).hover(
                     function () {
                        $(x).css({ backgroundColor: "#000", color: "#3ABCEF"});
                        $(y).show();
                     },
                     function () {
                        $(x).css({ backgroundColor: "#333", color: "#FFF"});
                        $(y).hide();
                     }
                );
                 $(y).hover(
                     function () {
                        $(x).css({ backgroundColor: "#000", color: "#3ABCEF"});
                        $(y).show();
                     },
                     function () {
                        $(x).css({ backgroundColor: "#333", color: "#FFF"});
                        $(y).hide();
                     }
                );
            })();
        }
    }); 
});

Here's what I really want:

var menu=new Array("#power","#services","#cashback","#schedule");

$(document).ready(function(){
    $(function() {
        for(var i in menu)
        {
            (function() {
                var x = menu[i];
                var y = menu[i]+'_menu';
                 $(x,y).hover(
                     function () {
                        $(x).css({ backgroundColor: "#000", color: "#3ABCEF"});
                        $(y).show();
                     },
                     function () {
                        $(x).css({ backgroundColor: "#333", color: "#FFF"});
                        $(y).hide();
                     }
                );
            })();
        }
    }); 
});

UPDATE ::: Here is the final working implimentation:

var menu=new Array("#power","#services","#cashback","#schedule");

$(document).ready(function(){
    for(var i in menu)
    {
        (function(x, y) {
             $(x+','+y).hover(
                 function () {
                    $(x).css({ backgroundColor: "#000", color: "#3ABCEF"});
                    $(y).show();
                 },
                 function () {
                    $(x).css({ backgroundColor: "#333", color: "#FFF"});
                    $(y).hide();
                 }
            );
        })(menu[i], (menu[i] + '_menu'));
    }
});
3
  • 1
    $(menu.join(', ')) to listen for an event on the primary elements, and then grab the secondary element by parsing out the ID of the first element. Commented Sep 27, 2012 at 1:22
  • Please do not update the question's code based on the answers. Commented Sep 27, 2012 at 1:35
  • @undefined oops ok ill put it back! Commented Sep 27, 2012 at 1:39

4 Answers 4

3

alternative variable x and y set function arguments run the @param

(function(x, y) {

             $(x+','+y).hover(
                 function () {
                    $(x).css({ backgroundColor: "#000", color: "#3ABCEF"});
                    $(y).show();
                 },
                 function () {
                    $(x).css({ backgroundColor: "#333", color: "#FFF"});
                    $(y).hide();
                 }
            );
})(menu[i], (menu[i] + '_menu'));
Sign up to request clarification or add additional context in comments.

Comments

1

Don't call $ with two arguments, join them in a string:

$(x + "," + y).hover(

This way you'll get the selector you want: "#power,#power_menu" etc. Calling $ with two arguments would only use the first as a selector, treating the second as a context to select from.

2 Comments

lol man o man. it was simple as that. i thought it might have to do with the var already being allocated. thankyou!
@DanKanze Just one more thing: calling $(document).ready(fn) and $(fn) have the same effect. There's no need to do both.
1
$(menu.join(', ')).hover();

Then inside the anonymous functions called by hover:

var _this = $(e.target);
var id = _this.attr('id');
var secondary_elm = $('#' + id + '_menu');
secondary_elm.show();

End result something like this:

             $(menu.join(', ')).hover(
                 function (e) {
                    var _this = $(e.target);
                    var id = _this.attr('id');
                    var secondary_elm = $('#' + id + '_menu');
                    secondary_elm.show();
                 },
                 function (e) {
                    var _this = $(e.target);
                    var id = _this.attr('id');
                    var secondary_elm = $('#' + id + '_menu');
                    secondary_elm.show();
                 }
            );

This leaves out the secondary element for being hovered on. Without understanding the HTML structure, I would imagine this is something you'd want to re-examine. Working with the .hover() event is a pain to begin with. When you start listening to multiple different elements to do the same thing essentially, you're asking for a whole heck of a lot of confusion.

2 Comments

the hover() is actually needs to bind on html elements independent of each other. thats why this seems rediculous. however, the layout is using several responsive design implimentations thus the need for the additional complexity.
also, joining the array will not work in my case becuase the "_menu" is independent of menu[i]. i chose to do it this way becuase not every menu[i] has a "_menu". sorry i should have mentioned that.
0
jQuery(function($) {

    // put this in your loop body:

    var $x = $('#x');
    var $y = $('#' + $x.attr('id') + '_menu');

    function mouseIn(event) {
        var $this = $(event.target);    // x or y
        $x.css({ backgroundColor: "#000", color: "#3ABCEF"});
        $y.show();
    }

    function mouseOut(event) {
        var $this = $(event.target);    // x or y
        $x.css({ backgroundColor: "#333", color: "#FFF"});
        $y.hide();
    }

    $x.add($y).hover(mouseIn, mouseOut);
});

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.