javascriptareacoords

Change coords of area tag using javascript


I'm trying to change the coords of an area tag using JavaScript. Here is my HTML code:

<a onclick="changeCoords('area-1', '50,825,220,1225');">Button</a>                      
<img id="page-1" class="shadow margin-20 width-900" src="pics/pages/razei-harefua-1.jpg"     usemap="#research-map">
<map name="research-map">
    <area id="area-1" shape="rect" coords="110,980,390,1150" href="some URL">
</map> 

Here is the JavaScript:

function changeCoords(areaID, newCoords) {
    var x = document.getElementById(areaID).coords;
    x = newCoords;
}

Solution

  • If you are trying to just change the coords property with the newCoords, then simply write:

    document.getElementById(areaID).coords = newCoords
    

    Your example doesn't work because you are assigning the value of coords to the x variable, and then you just change the value of x to the newCoords value. The x variable isn't a reference to the coords property, is just an allocated memory space that in your case stores the value of coords, and then that value is overwritten with the value of newCoords.