How to dynamically change image source in HTML?

Please help me with this question. I am trying to change the image source dynamically.

+6
html
source share
1 answer

Client (dynamic) image replacement ...

You will need to use javascript to do this:

<img src="image1.jpg" id="myImage" />

 <script type="text/javascript"> document.getElementById("myImage").src = "image2.jpg"; </script> 

Introducing jQuery ...

If this is the type of answer you are looking for, I would also like to invite you to start checking and a javascript framework like jQuery , which makes this type of thing a lot easier to execute / manage. In jQuery, you can accomplish the same thing with the following code:

 $("#myImage").attr("src", "image2.jpg"); 
+13
source share

All Articles