Reputation: 103
Good day,
I was playing around with SVG, when I noticed this behaviour.
Background: I was loading the SVG using snap.svg. The node I wanted to replace was inside a group <g> node.
I selected an SVG node using document.getElementById. Then I got the parent. Then I replaced the textContent property of the parent with another set of SVG text. However, the new SVG was not visible.
I opened the developer tools in google chrome. I selected the same node in the node tree view. I edited the node as HTML, replacing all SVG nodes with the new SVG text. However, the new SVG was not visible.
Lastly, I selected the root element, the SVG tag itself. Then edited the SVG tag as HTML, appending the SVG text as the last child element. Now the new SVG was visible?
Are there SVG drawing rules that I should be aware of? I've read in other questions that it's hard/impossible to replace SVG text the save way HTML can be replaced by using node.innerHTML.
Upvotes: 1
Views: 156
Reputation: 60996
Have you looked at using Element.innerSVG which is provided by Snap.svg?
Also, Element.innerHTML
should work on svg elements too, and it already does work in some browsers (e.g newer versions of Opera and Chrome, see here). This is defined in the DOM Parsing spec.
Upvotes: 1
Reputation: 1307
You should try creating the SVG elements with the namespace aware createElementNS
API. For an example, see http://www.kevlindev.com/tutorials/basics/shapes/js_dom/
Upvotes: 0