jquery animation callback - how to pass parameters to callback

Posted by Hans on Stack Overflow See other posts from Stack Overflow or by Hans
Published on 2010-03-30T14:50:10Z Indexed on 2010/03/30 14:53 UTC
Read the original article Hit count: 583

Filed under:
|
|
|

I´m building a jquery animation from a multidimensional array, and in the callback of each iteration i would like to use an element of the array. However somehow I always end up with last element of the array instead of all different elements.

html:

<div id="square" style="background-color: #33ff33; width: 100px; height: 100px; position: absolute; left: 100px;"></div>

javascript:

$(document).ready(function () {

// Array with Label, Left pixels and Animation Lenght (ms)
LoopArr = new Array(
    new Array(['Dog', 50, 500]),
    new Array(['Cat', 150, 5000]),
    new Array(['Cow', 200, 1500])
);

$('#square').click(function() {

for (x in LoopArr) {
    $("#square").animate({ left: LoopArr[x][0][1] }, LoopArr[x][0][2], function() {
        alert (LoopArr[x][0][0]);
    });
}

});

});

`

Current result: Cow, Cow, Cow

Desired result: Dog, Cat, Cow

How can I make sure the relevant array element is returned in the callback?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery