-1

If you have an image element, say an SVG. How do you retrieve the image code, in such a way that the output could look like the following

<svg ...></svg>
PM 77-1
  • 12,933
  • 21
  • 68
  • 111
  • 1
    What exactly are you trying to achieve? – PM 77-1 Jun 06 '19 at 14:59
  • The same as you would for any other element? `$("svg")`? – Lewis Jun 06 '19 at 15:02
  • Also - Possible duplicate of [JQuery and SVG - how to find the 'g' tag](https://stackoverflow.com/questions/17043318/jquery-and-svg-how-to-find-the-g-tag) – Lewis Jun 06 '19 at 15:02
  • Is that exactly what you're looking for? [Conversion of JPEG to SVG in Javascript - Stack Overflow](https://stackoverflow.com/questions/4022774/conversion-of-jpeg-to-svg-in-javascript#41182213) – Abdulsamet Kurt Jun 06 '19 at 15:02

1 Answers1

0

Assuming you are using an img tag that looks like this

<img src="whatever.svg">

you could find the value of src and perform a fetch to that URL to get the underlying XML:

async function getSVGXML(selector) {
  const url = $(selector).attr('src');
  return await fetch(url).then(res => res.text());
}
Caleb Williams
  • 1,035
  • 5
  • 12