Reputation: 816
I'm using jquery's each() to add click events to a set of imgs , this works fine but i want each img to have a different effect on click it .
$('#worksfoot img').each( function() {
$(this).click( function() {
$('#entrycontainer').animate({
marginLeft:
},500); })
})
I would like for the first img to set marginLeft as 0 , then increment it 100 for each of the others .
Upvotes: 5
Views: 23546
Reputation: 25247
You could try the following solution:
$('#worksfoot img').each( function(index) {
$(this).click( function() {
$('#entrycontainer').animate({
marginLeft: 100*index
},500);
});
});
Upvotes: 7
Reputation: 11
You shouldn't use multiple id's for different elements. To make it work fine and always try using class instead of id.
Upvotes: 1
Reputation: 837
('#worksfoot img').each( function(index, elem) {
elem.click( function() {
$('#entrycontainer').animate({
marginLeft: 100*index
},500); })
})
Upvotes: 1
Reputation: 480
$('#worksfoot img').each( function(index) {
$(this).click( function() {
$('#entrycontainer').animate({
marginLeft: index * 100
},500); })
})
Upvotes: 0