Change the URL of an image:
document.getElementById("myImg").src = "image2.png";
Click the button to change the value of the src attribute of the image.
<!DOCTYPE html> <html> <body> <img id="myImg" src="image1.png" width="100" height="98"> <button onclick="myFunction()">Test</button> <script> function myFunction() {//from w w w . j a v a2 s . c o m document.getElementById("myImg").src = "image2.png"; } </script> </body> </html>
The src
property sets or gets the value of the src attribute of an image.
The required src attribute specifies the URL of an image.
The src
property accepts and returns a String type value.
Value | Description |
---|---|
URL | Specifies the URL of the image. |
The src
property returns a String representing the URL of the image.