JSDriller
JSDriller

Reputation: 97

Dynamically created SVG not working in Firefox, but works in Chrome

I am trying to figure out why this code of is not working in Firefox. It's supposed to create horizontal paths, but I cannot see them in Firefox. Chrome and IE showing them properly. What could be the issue?

https://jsfiddle.net/7a6qm371/

<div>
<svg width="100%" height="500" id="svgBottomWall">
    <g style="stroke: aqua; fill: none;" id="svgBottomWallGridGroup"></g>
</svg>

$(document).ready(function () {

var svgBottomWall = document.getElementById("svgBottomWall");
var rect = svgBottomWall.getBoundingClientRect();
var svgW = rect.width;



function createHorizontalLine(w, d) {
    var nline = document.createElementNS("http://www.w3.org/2000/svg", "path");
    nline.setAttribute("d", "M 0 " + d + ", L " + w + " " + d);
    nline.setAttribute("stroke-width", 1);
    nline.setAttribute("stroke", "#ffffff");
    document.getElementById("svgBottomWallGridGroup").appendChild(nline);
}
for (var i = 0; i <= svgW; i = i + 100) {
    createHorizontalLine(svgW, i);
}
});

Upvotes: 5

Views: 756

Answers (1)

Phil
Phil

Reputation: 164813

Your d path parameters are incorrectly formatted.

You have something like

d="M 0 100, L 1000 100"

whereas it should be

d="M 0,100 L 1000,100"

See https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/d

The fix is

nline.setAttribute("d", "M 0," + d + " L " + w + "," + d);

JSFiddle

Upvotes: 3

Related Questions