To modify the value of an HTML attribute, use the following syntax:
| document.getElementById(id).attribute = new value |
This example updates the value of the src attribute of an <img> element:
| <!DOCTYPE html> <html> <body> <img id=”myImage” src=”smiley.gif”> <script> document.getElementById(“myImage”).src = “landscape.jpg”; </script> </body> </html> |
In the example, an HTML <img> element with id="myImage" is accessed via the DOM, and JavaScript changes its src attribute from "smiley.gif" to "landscape.jpg".