Site icon Puneet Sharma – Freelance Web Developer

Replace an SVG image by SVG code – Script

Here is a script that will help you replace an image tag with an SVG extension image file with SVG code. This can be helpful when you don’t want too much SVG code inside your HTML file.

(function () {
  console.log(document.getElementById("svg1Image").querySelector("img").src);
  fetch(document.getElementById("svg1Image").querySelector("img").src)
    .then((response) => response.text())
    .then((svgCode) => {
      document.getElementById("svg1Image").querySelector("img").remove();
      var divElement = document.getElementById("svg1Image");
      divElement.innerHTML = svgCode;
    })
    .catch((error) => {
      console.error("Error fetching SVG image:", error);
    });
})();
Exit mobile version