Access Multiple Elements of same ID in jQuery

Do not create markup that contains elements with duplicate IDs. This will break things, and you will be mauled by a velociraptor faster than you can say "goto".

Use classes instead:

<img src="https://stackoverflow.com/questions/4789859/0.jpg" id='images' />
<img src="..." class="myEle" />
<img src="..." class="myEle" />

then…

$(document).ready(function() {
    $('.myEle').live('mouseup', function () {

        $('#images').attr("src", myEle.getNumber() + ".jpg"); 
    });
});

Re: OP comment

“How do i know which image is pressed?”

Use the this keyword:

$(document).ready(function() {
    $('.myEle').live('mouseup', function () {

        $('#images').attr("src", $(this).attr('src')); 
    });
});

…I think that’s what you’re looking for.

velociraptor

Leave a Comment