0

i am new with jQuery and i want to make an array to be executed.So i make an array of same class of divs.those are like that

<div class="block"><img src="/home/ranjit/Desktop/Test/sunrise1.jpg" alt="sunset" /></div>
<div class="block"><img src="/home/ranjit/Desktop/Test/sunset-sailing.jpg" alt="sunset1" /></div>

look that each of div having different data in an array and i make the array like this

var matches = [];
jQuery(".block").each(function() {
    matches.push(this);
});

now i want to run the function

function slider() {
    for (var i= 0; i < len; i++){
      console.log(matches[i]);
      jQuery(matches[i]).show("slow");
      jQuery(matches[i]).animate({left:'+=730'}, 3000, function(){
        jQuery(this).animate({left:'-=730'},3000);
      });
    }
  }

when i call the the slider function it executed the last of the div only. why i dont know, but i think it should logically executed the first div then the second div. plz help me why is this not executed like that .

1
  • 1
    no sense pushing them into an array if you're just going to loop over them immediately. Actually.. $('.block') already behaves like an array, so what you're doing is completely redundant. Just assign it to a variable if you really need to. Commented Feb 4, 2012 at 5:39

1 Answer 1

3

Your for loop has i < len but you didn't define len anywhere.

You probably want i < matches.length

As an alternative to the for loop, use jQuery.each

Another potential issue is that you need to queue the animation effects. from jQuery.animate:

queue: A Boolean indicating whether to place the animation in the effects queue. If false, the animation will begin immediately. As of jQuery 1.7, the queue option can also accept a string, in which case the animation is added to the queue represented by that string.

Sign up to request clarification or add additional context in comments.

2 Comments

yes, i use len as len = matches.length; but the jQuery.each function didnot work like that i want. i want that the first div is executed first when the for loop condition meet true, then the second one.
You may need to queue the animations. See my edits to the answer above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.