jeffreynolte
jeffreynolte

Reputation: 3779

Calling function within .each()

I would like to repeat the chained events starting at .animate() from inside itself (indicated by comment). What is the correct way to write this?

$("div").each(function(i, e){  
  $(this).animate({ left: $(this).parent().width() }, d[i], "linear",
      function(){ $(this).css({left: -parseInt($(this).css("width")) })
      //would like to call function at this stage
   });
})        

This ended up working:

   $("div").each( v = function(i, e){  
     $(this).animate({ left: $(this).parent().width() }, d[i], "linear",
         function(){ $(this).css({left: -parseInt($(this).css("width")) })
         v();
      });
   });          

Upvotes: 4

Views: 202

Answers (1)

mattsven
mattsven

Reputation: 23253

Like this?

$("div").each(function(i, e){  
  $(this).animate({ left: $(this).parent().width() }, d[i], "linear",
      v = function(i){
            $(this).css({left: -parseInt($(this).css("width")) })
            if(i == null) v(1);
        });
})   

Not sure I fully understood your goal...

Upvotes: 2

Related Questions