Ala Ala - 1 month ago 9
Javascript Question

JS Exception : animate is not a function

I've an image inside a

div
, and the following Javascript code:

function animate_down(key1, key2)
{
cimage = document.getElementById('sslot_img' + key1);
var topval = cimage.offsetTop;
if (topval == -416) {
cimage.animate({ top: '0px' }, 5000);
}
else {
topval = topval - 32;
var toptxt = { 'top': topval.toString() };
cimage.animate(toptxt, 5000);
}
}


I've test it in Firebug, when the debugger reaches
.animate
function it's throwing :

cimage.animate is not a function


what am i missing?

Answer

getElementById is not a jQuery function, and will not yield a jQuery object. animate is only available on jQuery objects. You need to either wrap your cimage:

$(cimage).animate( ... )

or fetch it through jQuery rather than getElementById

var cimage = $('#sslog_img' + key1);

Note that if you go with the latter option, cimage will not have an offsetTop property, but rather, you'd have to use

cimage.offset().top

or

cimage[0].offsetTop