Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an easy way to clear an SVG element's contents?

In HTML, I can clear a <div> element with this command:

div.innerHTML = ""; 

Is there an equivalent if I have an <svg> element? I can't find an innerHTML nor innerXML or even innerSVG method.

I know the SVG DOM is a superset of the XML DOM, so I know I can do something like this:

while (svg.lastChild) {     svg.removeChild(svg.lastChild); } 

But this is both tedious and slow. Is there a faster or easier way to clear an SVG element?

like image 381
Aseem Kishore Avatar asked Sep 09 '10 06:09

Aseem Kishore


1 Answers

If you're using jQuery, you can just do

$("#svgid").empty(); 

This deletes all child elements of the svg while leaving its other attributes like width and height intact.

like image 154
FrancesKR Avatar answered Oct 12 '22 20:10

FrancesKR