Alex Derkach
Alex Derkach

Reputation: 21

function for each element

Ive got a function:

setInterval ( doSomething, 100 );

function doSomething ( )
{
$("#i_msl").animate({ top: "-150px",}, 1000 ).delay(1000);
$("#i_msl").animate({ top: "-300px",}, 1000 ).delay(1000); 
$("#i_msl").animate({ top: "0px",}, 1000 ).delay(1000); 
}

but it works only for one element. How can I make it work for all elements with #i_msl on page?

Upvotes: 2

Views: 157

Answers (4)

Justin Ethier
Justin Ethier

Reputation: 134167

The selector #i_msl is for a specific ID; these are supposed to be unique on the page. Instead, assign each element a unique class, such as:

class="my-class"

Then just select them using a class selector:

$(".my-class").animate({ top: "-150px",}, 1000 ).delay(1000);
$(".my-class").animate({ top: "-300px",}, 1000 ).delay(1000); 
$(".my-class").animate({ top: "0px",}, 1000 ).delay(1000); 

Upvotes: 2

zzzzBov
zzzzBov

Reputation: 179046

#i_msl is an ID selector. You can (should) only have a single item on the page with a particular id. If you need multiple elements to match the selector, use a class: .i_msl

Also, setInterval will be called every 100ms which may cause issues with execution when your animation takes longer than the interval.

Upvotes: 1

wanovak
wanovak

Reputation: 6127

You can't. You can only have one unique id per page. Change it from id to class, and target by $('.i_msl').

Upvotes: 2

CassOnMars
CassOnMars

Reputation: 6181

You are assigning an ID to multiple elements. You can't do that. Try giving them a class instead:

class="i_msl"

You can access it with $(".i_msl")

Upvotes: 3

Related Questions