views:

47

answers:

4

I am trying to create and array of objects so that I can access them in a for loop in jQuery and I know that this works in Actionscript so what I am trying to do is convert my current knowledge to jQuery that will work.

Please have a look at this and tell me why I cannot access divToShow

Thanks guys

var homeImages = new Array();
homeImages[0] = { hoverImage: ".leftColImage1", divToShow: ".image1", rollOverImg: "img-family-over.jpg" };
homeImages[1] = { hoverImage: ".leftColImage2", divToShow: ".image2", rollOverImg: "img-students-over.jpg" };
homeImages[2] = { hoverImage: ".leftColImage3", divToShow: ".image3", rollOverImg: "img-pros-over.jpg" };
homeImages[3] = { hoverImage: ".leftColImage4", divToShow: ".image4", rollOverImg: "img-retired-over.jpg" };

var hoverImage;
var activeDiv;
var mainContent = ".mainContent";

for (k = 0; k < homeImages.length; k++) {

    homeImages[k].id = k;
    $(homeImages[k].hoverImage).mouseover(function() {
    //alert("divToShow : " + homeImages[this.id].divToShow);
        alert("this : " + this.id);
        activeDiv = homeImages[k].divToShow;
        $(".leftColImage1 img").attr("src", "/App_Themes/MyChoice2010/Images/" + homeImages[k].rollOverImg);
        $(mainContent).hide();
        $(homeImages[k].divToShow).slideDown("slow");
    }).mouseout(function() {
        $(".leftColImage1 img").attr("src", "/App_Themes/MyChoice2010/Images/img-family.jpg");
        $(".image1").hide();
        $(mainContent).slideDown("slow");
    });
}
A: 
//alert("divToShow : " + homeImages[this.id].divToShow);

In that context, this refers to the current HTML element, not the current homeImages element.

Eric
A: 

The reason is the old classic relating to closures: in the mouseover handler, k is always set to its last value of 4 rather than its value when the mouseover handler was created, which is what your code is expecting.

You can fix this by creating the mouseover handler in a function:

function addMouseEventHandlers(imageIndex) {
    var homeImage = homeImages[imageIndex];
    homeImage.id = imageIndex;
    $(homeImage.hoverImage).mouseover(function() {
    //alert("divToShow : " + homeImages[this.id].divToShow);
        alert("this : " + this.id);
        activeDiv = homeImage.divToShow;
        $(".leftColImage1 img").attr("src", "/App_Themes/MyChoice2010/Images/" + homeImage.rollOverImg);
        $(mainContent).hide();
        $(homeImage.divToShow).slideDown("slow");
    }).mouseout(function() {
        $(".leftColImage1 img").attr("src", "/App_Themes/MyChoice2010/Images/img-family.jpg");
        $(".image1").hide();
        $(mainContent).slideDown("slow");
    });
}

for (k = 0; k < homeImages.length; k++) {
    addMouseEventHandlers(k);
}
Tim Down
A: 

You are accessing the variable k from inside the mouseover handler function. But by the time that function is called, the value of k has already changed and is now equal to homeImages.length since the for loop has already run to completion.

One way to solve this is to use $.each instead of the for loop:

$.each(homeImages, function(k, element) {
    element.id = k;
    $(element.hoverImage).mouseOver(function() {
        .... //you can use the value of k or element here
    });
});

This will work because the function passed to $.each creates a new closure which remembers the value of k for each iteration.

interjay
A: 

Ok, try this:

for (k = 0; k < homeImages.length; k++) {
    (function(current) {
        $(current.hoverImage).hover(function() {
            activeDiv = current.divToShow;
            $(".leftColImage1 img").attr("src", "/App_Themes/MyChoice2010/Images/" + current.rollOverImg);
            $(mainContent).hide();
            $(current.divToShow).slideDown("slow");
        },
        function() {
            $(".leftColImage1 img").attr("src", "/App_Themes/MyChoice2010/Images/img-family.jpg");
            $(".image1").hide();
            $(mainContent).slideDown("slow");
        });
    })(homeImages[k]);
}

Or alternatively:

function setUpHover(item) {
    $(item.hoverImage).hover(function() {
        activeDiv = item.divToShow;
        $(".leftColImage1 img").attr("src", "/App_Themes/MyChoice2010/Images/" + current.rollOverImg);
        $(mainContent).hide();
        $(item.divToShow).slideDown("slow");
    },
    function() {
        $(".leftColImage1 img").attr("src", "/App_Themes/MyChoice2010/Images/img-family.jpg");
        $(".image1").hide();
        $(mainContent).slideDown("slow");
    });
}

for (k = 0; k < homeImages.length; k++) {
    setUpHover(homeImages[k]);
}
Eric
Awesome, that worked! Thanks Eric. Still new to jQuery so it's boggles me some. :)
Sixfoot Studio
I think you're confusing jQuery with Javascript. jQuery is just a function library for javascript: the closures are a feature of javascript, not jQuery.
Eric
Did you? Sorry, but I didn't notice when I updated the answer. The first half was definitely posted before any other answer. I'm sorry if the second half is similar to yours, but I honestly derived it solely from my first solution.
Eric