Image alt Property - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Image


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>

<img id="myImg" src="" alt="alt message" width="107" height="98">

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

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

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


Related Tutorials