UPDATE I've compare objects in the debugger and it seems the map
element is not a child of the img
. However removing img.appendChild(map);
still didn't get it to work. To be complete - along with the accepted answer, I had to add div.appendChild(map);
so that the map has a parent.
I am trying to dynamically create an image area map. I have a jsFiddle of the code below: Here. I have successfully been able to generate a static map and bind a mouseover on the area via js, but I can't seem to create the map from scratch. I am using pure js. Any help would be appreciated.
<head>
<script>
function function1() {
var img = document.getElementById("myImg");
var map = document.createElement("map");
map.name = "myMap";
var area = document.createElement("area");
area.shape = "rect";
area.coords = "0,0,100,50"
area.onmouseover = function(){alert("over")};
map.appendChild(area);
var div = document.getElementById("div");
div.appendChild(map);
img.usemap = "#myMap";
}
</script>
</head>
<body>
<div id="div">
<img src="https://placeholdit.imgix.net/~text?txtsize=33&txt=350%C3%97150&w=350&h=150"
id="myImg"
alt="Image"
width="350"
height="150">
<br>
<input type="button" value="Make area active" onClick="function1();">
</div>
</body>