How can I change image source on click with jQuery?
You need to use preventDefault()
to make it so the link does not go through when u click on it:
fiddle: http://jsfiddle.net/maniator/Sevdm/
$(function() {
$('.menulink').click(function(e){
e.preventDefault();
$("#bg").attr('src',"img/picture1.jpg");
});
});
It switches back because by default, when you click a link, it follows the link and loads the page. In your case, you don't want that. You can prevent it either by doing e.preventDefault(); (like Neal mentioned) or by returning false :
$(function() {
$('.menulink').click(function(){
$("#bg").attr('src',"img/picture1.jpg");
return false;
});
});
Interesting question on the differences between prevent default and return false.
In this case, return false will work just fine because the event doesn't need to be propagated.