Reputation: 955
how can i select the 3 balls in the same function call?
here is the fiddle link: http://jsfiddle.net/X3SVp/2/
function flipper(){
$('#ball_1, #ball_2').each.animate({
"left": '-90',
}, function(){
$('#ball_1, #ball_2').animate({
"left": '200',
}, flipper());
});
}
flipper();
Upvotes: 0
Views: 1026
Reputation: 7960
Do you want all instances of ball_#
? If so, you can use the "starts with" selector:
$("[id^='ball_']")
That will select all elements with an id
attribute that starts with "ball_".
Upvotes: 1
Reputation: 9947
you are close
$('#ball_1, #ball_2, #ball3, #ball4').animate({left : -90}, function() {
$(this).animate({left: 200}, flipper);
});
, is used to have work on multiple, each is not needed in this case
Upvotes: 1
Reputation: 318352
function flipper(){
$('#ball_1, #ball_2, #ball_3').animate({left : -90}, function() {
$(this).animate({left: 200}, flipper);
});
}
You also need to add a position to all balls, and an initial left value, otherwise it won't work as jQuery doesn't have a starting position, and elements with a static position doesn't move.
Upvotes: 4