Javascript Reference - HTML DOM Area coords Property








The coords attribute specifies the x and y coordinates of an area.

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

Browser Support

coords Yes Yes Yes Yes Yes

Syntax

Return the coords property:

var v = areaObject.coords;

Set the coords property:

areaObject.coords = value;




Property Values

Value Description
x1,y1,x2,y2 for rect, it sets the top-left corner and the bottom-right corner of the rectangle
x,y,radius for circle, it sets the circle center and the radius
x1,y1,x2,y2,..,xn,yn for poly, it sets the edges of the polygon.

Return Value

A String representing the coordinates.

Example

The following code shows how to get the coordinates for a specific area in an image-map.


<!DOCTYPE html>
<html>
<body>
<!--from   w ww. ja  va2  s.c o 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() {
    var x = document.getElementById("myArea").coords;
    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 coordinates for a specific area in an image-map.


<!DOCTYPE html>
<html>
<body>
<!--  w  w w.  j  a  va2 s .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="130,60,8" alt="myImage" href="http://example.com">
</map>
<button onclick="myFunction()">test</button>

<script>
function myFunction() {
    document.getElementById("myArea").coords = "100,60,3";
    document.getElementById("demo").innerHTML = "change";
}
</script>

</body>
</html>

The code above is rendered as follows: