Javascript Reference - HTML DOM Area alt Property








The alt property sets or gets the value of the alt attribute of an area.

Browser Support

alt Yes Yes Yes Yes Yes

Syntax

Return the alt property:

var v = areaObject.alt;

Set the alt property:

areaObject.alt = text;

Property Values

Value Description
text The alternate text for the area




Return Value

A String representing an alternate text for the area.

Example

The following code shows how to get the alternate text of a specific area in an image-map.


<!DOCTYPE html>
<html>
<body>
<img src="http://java2s.com/style/demo/border.png" 
     width="145" height="126" usemap="#myImageMap">
<map name="myImageMap">
  <area id="myArea" shape="circle" coords="124,58,8" alt="an image" href="http://example.com">
</map><!--  www .j  a  v a2s. c o m-->
<p id="demo"></p>
<button onclick="myFunction()">test</button>
<script>
function myFunction() {
    var x = document.getElementById("myArea").alt;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to change the alternate text for a specific area in an image-map.


<!DOCTYPE html>
<html>
<body>
<!-- ww w.  j a  va2s .  co  m-->
<img src="http://java2s.com/style/demo/border.png" 
     width="145" height="126" usemap="#myImageMap">

<map name="myImageMap">
  <area id="myArea" shape="circle" coords="124,58,8" alt="An image" href="http://example.com">
</map>
<p id="demo"></p>
<button onclick="myFunction()">test</button>

<script>
function myFunction() {
    document.getElementById("myArea").alt = "your message";
    document.getElementById("demo").innerHTML = "changed";
}
</script>

</body>
</html>

The code above is rendered as follows: