Infra Stank
Infra Stank

Reputation: 816

using jquery's each() to add click events to imgs

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

Answers (4)

Enrique Moreno Tent
Enrique Moreno Tent

Reputation: 25247

You could try the following solution:

$('#worksfoot img').each( function(index) {
    $(this).click( function() {
        $('#entrycontainer').animate({
            marginLeft: 100*index
        },500);
    });
});

Upvotes: 7

Burning
Burning

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

Prashanth Shyamprasad
Prashanth Shyamprasad

Reputation: 837

('#worksfoot img').each( function(index, elem) {
    elem.click( function() {
        $('#entrycontainer').animate({
        marginLeft:  100*index
        },500); })
})

Upvotes: 1

f0rza
f0rza

Reputation: 480

$('#worksfoot img').each( function(index) {

    $(this).click( function() {

        $('#entrycontainer').animate({
             marginLeft: index * 100 

        },500); })



})

Upvotes: 0

Related Questions