Image alt Property - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Image

Description

The alt property sets or gets the alt attribute of an image.

Set the alt property with the following Values

Value Description
text Sets an alternate text for an image.

Return Value

A String, representing the alternate text of the image

The following code shows how to return the alternate text of an image:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<img id="myImg" src="http://java2s.com/resources/a.png" alt="alt message" width="107" height="98">

<button onclick="myFunction()">Test</button>

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

<script>
function myFunction() {/*from  w  ww  . java  2s.  c om*/
    var v = document.getElementById("myImg").alt;
    document.getElementById("demo").innerHTML = v;
}
</script>

</body>
</html>

Related Tutorials