Javascript DOM HTML Attribute value Property set

Introduction

Change the value of the src attribute of an <img> element:

Before changing the style:

Click the button to change the src attribute's value of the image above.

View in separate window

<!DOCTYPE html>
<html>
<body>

<img src="image1.png" width="100" height="100">
<button onclick="myFunction()">Test</button>

<script>
function myFunction() {/*from   w w w .j a v a 2 s  .  c  o  m*/
  var x = document.getElementsByTagName("IMG")[0];
  x.getAttributeNode("src").value = "image2.png";
}
</script>

</body>
</html>



PreviousNext

Related