Javascript DOM HTML Image alt Property set

Introduction

Change the alternate text of an image:

document.getElementById("myImg").alt = "newAlternateText";

Click the button to change the alternate text of the image.

View in separate window

<!DOCTYPE html>
<html>
<body>

<img id="myImg" src="image1.png" alt="The Circle" width="100" height="98">
<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {//from   ww w  .j  a v a 2  s  . c o  m
  document.getElementById("myImg").alt = "newAlternateText";
  document.getElementById("demo").innerHTML = "The value of the alt attribute was changed.";
}
</script>

</body>
</html>



PreviousNext

Related