How can I change an image after running a jquery function? I have an slideToggle function connected to the click of a div element containing the text and the image of an arrow, which I want to change into a different image when clicked.
HTML
<div id="italia">
CONCESSIONI NAZIONALI<img id="freccia" src="freccia_su.jpg" >
</div>
JQUERY
$("#nazione").click(function(){
$(".estero").slideToggle("slow", function() {
});
});
I tried adding this statement, but it doesn’t work:
$('#freccia').attr('src','freccia_giu.jpg');
3
Answers
Here is a working example of changing the image using links.
In your case you should add
/
to the beggining of your path in$('#freccia').attr('src','freccia_giu.jpg');
so it should look like this
$('#freccia').attr('src','/freccia_giu.jpg');
It depends on where the image is saved inside your project.
If it’s inside the root folder then
'freccia_giu.jpg'
should work just fine, but incase it’s inside some other directory you need to define the path to the image. For example'/images/freccia_giu.jpg'
Edit:
If you want to alternate between the images you can do this :
It should work if you put the expression
$("#freccia").attr("src",<newurl>);
in the right place: