0

The function below is meant to insert a <polygon></polygon> into an <svg id = "svg-overlays"></svg>.

I can see that the element gets successfully inserted when the function is called. However, the polygon is still not visible. If I do the insertion manually within the html document I get the desired polygon.

Unable to determine what the issue is here.

 function changeFloorImage(floor) {

        var overlays = document.getElementById("svg-overlays");
        var polygon = document.createElement("polygon");

        polygon.className = "apartment-overlay";
        polygon.setAttribute("points", "740,88 972,88 972,353 740,353");

        overlays.appendChild(polygon);
    };
Arshavsky
  • 13
  • 3
  • Tags are markup, the DOM has elements (hence *createElement*). ;-) – RobG Sep 04 '15 at 02:07
  • you need to create svg element like this: `document.createElementNS("http://www.w3.org/2000/svg", "polygon");` – gp. Sep 04 '15 at 02:16

1 Answers1

0

Try to use

document.createElementNS("http://www.w3.org/2000/svg", "polygon");

You need to specify SVG name space. There are several examples in stackoverflow.

Creating SVG elements dynamically with javascript inside HTML

Add SVG element to existing SVG using DOM

Community
  • 1
  • 1
Kagan Agun
  • 489
  • 1
  • 6
  • 7