The Area object represents an HTML <area> element.
The Area object supports the standard properties and events.
We can access an <area> element by using getElementById().
<!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="myImage" href="http://example.com">
</map><!-- w w w.j av a 2 s . c om-->
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementById("myArea").href;
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
The code above is rendered as follows:
We can create an <area> element by using the document.createElement() method.
<!DOCTYPE html>
<html>
<body>
<!-- w w w . j a va 2 s. c o m-->
<img src="http://java2s.com/style/demo/border.png" width="150" height="150" usemap="#myImageMap">
<map id="myMap" name="myImageMap"></map>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.createElement("AREA");
x.setAttribute("href", "http://example.com");
x.setAttribute("shape", "circle");
x.setAttribute("coords", "130,60,8");
document.getElementById("myMap").appendChild(x);
document.getElementById("demo").innerHTML = "created";
}
</script>
</body>
</html>
The code above is rendered as follows:
Property | Description |
---|---|
alt | Sets or gets the alt attribute of an area |
coords | Sets or gets the coords attribute of an area |
hash | Sets or gets the anchor part of the href attribute value |
host | Sets or gets the hostname and port in the href attribute value |
hostname | Sets or gets the hostname in the href attribute value |
href | Sets or gets the href attribute of an area |
noHref | Not supported in HTML5. Sets or gets the nohref attribute of an area |
Returns the protocol, hostname and port in the href attribute value | |
password | Sets or gets the password in the href attribute value |
pathname | Sets or gets the pathname from the href attribute value |
port | Sets or gets the port from the href attribute value |
protocol | Sets or gets the protocol from the href attribute value |
search | Sets or gets the query string from the href attribute value |
shape | Sets or gets the shape attribute of an area |
target | Sets or gets the target attribute of an area |
username | Sets or gets the username in the href attribute value |