s j s j - 6 months ago 9
Javascript Question

Jquery/javascript function issue

I have a javascript function doing some animation and its parameter is a parent selector.

function myFunction(a)
{
$("'"+a+">#one'").stop(true, true).animate({left:'30px'},1100);
$("'"+a+">#two'").stop(true, true).animate({left:'30px'},1100);
}


and the following line is calling that function.

$("#main").mouseleave(myFunction("#main"));


but this is not working, can anyone tell me whats wrong in my code ?

Answer

http://jsbin.com/ifunof/1/edit

function myFunction(a){
    $(a+">#one").stop().animate({left:30},1100); // <-- fixed selectors
    $(a+">#two").stop().animate({left:30},1100);
}

$("#main").mouseleave(function(){ // <-- (immediate call: Wrong)
  myFunction("#main");            // <-- fn reference: OK!
});
Comments