1

Having an img svg on the dom, is it possible to "extract" the code from it using javascript?

I want to be able to redraw it as an embed element and modify it using css and js.

<!-- turn something like this -->

<img src="https://www.seekicon.com/free-icon-download/stackoverflow-icon_1.svg" width="24" height="24">

<!-- into this -->

<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M0 0v24h24v-24h-24zm10.488 8.272l5.531 3.243-.686 1.162-5.532-3.243.687-1.162zm-1.456 3.113l6.185 1.739-.331 1.23-6.204-1.667.35-1.302zm-.672 2.813l6.498.65-.118 1.28-6.504-.586.124-1.344zm-.193 2.469h6.667v1.333h-6.667v-1.333zm8.833 3.333h-11v-7h1v6h9v-6h1v7zm-.852-8.704l-3.56-5.219 1.115-.76 3.559 5.219-1.114.76zm1.356-.841l-1.08-6.224 1.328-.231 1.081 6.224-1.329.231z"/></svg>
Mesalina
  • 11
  • 3
  • SVGs are created using elements https://developer.mozilla.org/en-US/docs/Web/SVG/Element. – Adam Azad Jul 31 '20 at 08:23
  • 2
    What have you already tried? SVGs are already elements within the DOM. – BenM Jul 31 '20 at 08:23
  • I edited my post to make it more clear. I know I can manipulate the svg if it's a proper svg element, but I want to convert an img into a svg, that's the question – Mesalina Jul 31 '20 at 08:31
  • 3
    [If it's on the same domain you can just do a normal ajax request for it, and get the file back as a string. This works for any kind of text file.](https://stackoverflow.com/questions/5448199/get-the-source-code-of-an-external-script) – enxaneta Jul 31 '20 at 08:33

0 Answers0