I made a quick "svg-include" service that simply replaces elements on the DOM that have a given attribute with some svg. However, I hit a road block when I tried to do the replace.
In order to useelement.replace()
, each node must be of type node
. How do I evaluate my SVG string so that I may replace an element with it?
Here's a snippet:
//Do eval here so that svg is Node not String
svg = SvgIncludeSevice.paths[i].svg;
//svg.className = results[j].className;
results[j].parentNode.replaceChild(svg, results[j]);
Thanks everybody!
BY THE WAY!
I will not be accepting answers stating to use JQuery of Prototype or anything else. I wish to use only native calls.
UPDATE
I tried to use this sloppy method I found to convert my string to manageable HTML, but it still didn't work! It silently failed...
This is what I changed:
hiddenDiv.innerHTML = SvgIncludeSevice.paths[i].svg;
svg = hiddenDiv.firstElementChild;
if(svg)
{
svg.className = results[j].className;
results[j].parentNode.replaceChild(svg, results[j]);
}
Oddly, it does evaluate my SVG and allow me to do the replace, which is awesome! Yet, the replace doesn't quite work. Look at these examples:
So, I do get a handle to my evaluated SVG node:
Yet after the replace occurs:
Obviously that didn't work...
Plus, I had to copy this out of my JSFiddle to test it. I didn't change the code at all, it just didn't work in JSFiddle for some reason... :(