jQuery puts the dynamically generated elements in the HTML namespace, but they have to be in the SVG namespace. You see that if you add the following code after the section $('#drawing').append(...)
:
alert((new XMLSerializer()).serializeToString(document.getElementById("drawing")));
This will give you something like:
<svg style="width:100%; height:100%; position:absolute; top:0; left:0; z-index:2" version="1.1"
xmlns="http://www.w3.org/2000/svg" id="drawing">
<path xmlns="http://www.w3.org/1999/xhtml" stroke-width="3" stroke="blue" d="M 0 0 L 0 0"
style="z-index:100" class="drawnLine" id="GanglionLine"/>
<circle xmlns="http://www.w3.org/1999/xhtml" r="5" cy="-999" cx="-999" style="z-index:101"
class="point draggable ui-widget-content" id="GanglionCircle"/>
</svg>
Note the <path xmlns="http://www.w3.org/1999/xhtml" ...>
part.
To work around this, you might resort to plain DOM manipulation methods, or you could use DOMParser
:
$('#drawing').append((new DOMParser()).parseFromString(
'<g xmlns="http://www.w3.org/2000/svg"><path id="' + part.id + 'Line" class="drawnLine" style="z-index:' + zindexParts++ + '" d="M 0 0 L 0 0" stroke="blue" stroke-width="3"/><circle id="' + part.id + 'Circle" class="point draggable ui-widget-content" style="z-index:' + zindexParts++ + '" cx="-999" cy="-999" r="' + part.line.end.circle.r + '" /></g>',
"image/svg+xml"
).documentElement);
Note that you have to wrap everything into a <g>
element for two reasons:
.parseFromString()
needs well formed XML syntax with exactly one root element- You need to declare the SVG namespace somewhere (but you could also do this on the
<circle>
and<path>
elements as well)
This gets you halfway there. Your code still produces a zero length line and an offscreen circle.