tuddy tuddy - 1 year ago 102
Javascript Question

Selecting SVG and path elements with JavaScript

I would like to select the last path in an SVG and remove it. I've tried a variety of methods, in pure javascript and in jquery and I don't seem to be able to access the SVG properly or its paths.


<div id="thesvgwrapper">
<svg id="thesvg"...><path ....></path><path ...></svg>

I CAN clear the SVG using:


I CAN see the SVG's contents using:

var svgwrapper = $('#svgwrapper');
var svgcontents = $(svgwrapper).html();

However, I CANNOT similarly select the SVG and see it's path contents...

my goal is something like

$('#thesvg path:last').remove();

thanks a million for any help

Answer Source

Here's executable code in pure JavaScript:

var paths = svgDoc.getElementsByTagName("path");
var last_path = paths[paths.length - 1];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download