Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript change image, href onclick = looking for simpliest method

What is the simpliest method to change image src, when I click on a href -with JQuery?
img id="my_image" class="icon" src="1.png" alt="1.png" border="0" />
< a href="#" id="1">
< a href="#" id="2">
< a href="#" id="3">
1.png, 2.png, 3.png

Href is OK with # or would be better to placce here some JS?

like image 554
Adrian Avatar asked Dec 22 '22 18:12

Adrian


1 Answers

You should .bind()help a .click()help event listener on your anchors. In that listener, you change the src attribute by invoking .attr()help

$('a').click(function(event) {
    $('#my_image').attr('src', function(i, src) {
        return event.target.id + '.png';
    });
    return false;
});
like image 120
jAndy Avatar answered Apr 27 '23 00:04

jAndy