Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

jquery

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 .

like image 475
Infra Stank Avatar asked Dec 02 '11 18:12

Infra Stank


3 Answers

You could try the following solution:

$('#worksfoot img').each( function(index) {
    $(this).click( function() {
        $('#entrycontainer').animate({
            marginLeft: 100*index
        },500);
    });
});
like image 111
Enrique Moreno Tent Avatar answered Nov 07 '22 08:11

Enrique Moreno Tent


('#worksfoot img').each( function(index, elem) {
    elem.click( function() {
        $('#entrycontainer').animate({
        marginLeft:  100*index
        },500); })
})
like image 32
Prashanth Shyamprasad Avatar answered Nov 07 '22 07:11

Prashanth Shyamprasad


You shouldn't use multiple id's for different elements. To make it work fine and always try using class instead of id.

like image 1
Burning Avatar answered Nov 07 '22 08:11

Burning