How do I add an svg html tree in javascript using innerHTML?

Viewed 605

<svg width="32" height="32" viewBox="0 0 32 32" fill="none" xmlns="http://www.w3.org/2000/svg">
  <g class="check-box">
    <path class="vector" d="M16 0C7.17725 0 0 7.17725 0 16C0 24.8228 7.17725 32 16 32C24.8228 32 32 24.8228 32 16C32 7.17725 24.8228 0 16 0Z" fill="#e8e8e8" />
    <path class="tick" d="M24.1094 12.6094L15.4426 21.2759C15.1826 21.5359 14.8413 21.6667 14.5 21.6667C14.1587 21.6667 13.8174 21.5359 13.5574 21.2759L9.22412 16.9426C8.70264 16.4214 8.70264 15.5786 9.22412 15.0574C9.74536 14.5359 10.5879 14.5359 11.1094 15.0574L14.5 18.448L22.2241 10.7241C22.7454 10.2026 23.5879 10.2026 24.1094 10.7241C24.6306 11.2454 24.6306 12.0879 24.1094 12.6094V12.6094Z" fill="#FAFAFA" />
  </g>
</svg>

I want to add this using innerHTML in javascript. I don't want to link the svg using img tag because I want to animate parts of the svg with pure css.

3 Answers

Something like this:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            background: white
        }
        /* This will color the icon to green */
        .cool-icon .vector { /* The class of the circle path */
            fill: green
        }
    </style>
</head>
<body>
    <h1>JS Icon Inserter</h1>
    <p>Next to this paragraph is the icon element with the "cool-icon" class.</p>
    <!-- Only put a class -->
    <i class="cool-icon"></i>
</body>
<script>
    // This JS will insert this icon into the "cool-icon"
    const icon = '<svg width="32" height="32" viewBox="0 0 32 32" fill="none" xmlns="http://www.w3.org/2000/svg"><g class="check-box"><path class="vector" d="M16 0C7.17725 0 0 7.17725 0 16C0 24.8228 7.17725 32 16 32C24.8228 32 32 24.8228 32 16C32 7.17725 24.8228 0 16 0Z" fill="#e8e8e8" /> <path class="tick" d="M24.1094 12.6094L15.4426 21.2759C15.1826 21.5359 14.8413 21.6667 14.5 21.6667C14.1587 21.6667 13.8174 21.5359 13.5574 21.2759L9.22412 16.9426C8.70264 16.4214 8.70264 15.5786 9.22412 15.0574C9.74536 14.5359 10.5879 14.5359 11.1094 15.0574L14.5 18.448L22.2241 10.7241C22.7454 10.2026 23.5879 10.2026 24.1094 10.7241C24.6306 11.2454 24.6306 12.0879 24.1094 12.6094V12.6094Z" fill="#FAFAFA" /></g></svg>'
    const iconElement = document.querySelector('.cool-icon')
    iconElement.innerHTML = icon
</script>
</html>

https://jsfiddle.net/j7s3fwcd/

u can do it like

var el='<svg width="32" height="32" viewBox="0 0 32 32" fill="none" xmlns="http://www.w3.org/2000/svg">'+
        '<g class="check-box">'+
        '  <path class="vector"'+
         ' d="M16 0C7.17725 0 0 7.17725 0 16C0 24.8228 7.17725 32 16 32C24.8228 32 32 24.8228 32 16C32 7.17725 24.8228 0 16 0Z"'+
         '  fill="#e8e8e8" />'+
                '    <path class="tick"'+
        'd="M24.1094 12.6094L15.4426 21.2759C15.1826 21.5359 14.8413 21.6667 14.5 21.6667C14.1587 21.6667 13.8174 21.5359 13.5574 21.2759L9.22412 16.9426C8.70264 16.4214 8.70264 15.5786 9.22412 15.0574C9.74536 14.5359 10.5879 14.5359 11.1094 15.0574L14.5 18.448L22.2241 10.7241C22.7454 10.2026 23.5879 10.2026 24.1094 10.7241C24.6306 11.2454 24.6306 12.0879 24.1094 12.6094V12.6094Z"'+
        ' fill="#FAFAFA" /></g> </svg>'

        document.getElementById("content").innerHTML=el;

I'm not sure to understand your issue. What's problem are you facing when using innerHTML?

const container = document.querySelector('#container');
container.innerHTML = '...';

Doesn't this works as expected?

Additionally, in case you have any modifications to do to this svg element or some events to listen to, I would recommend using multiples createElement to create your tags tree and then append this instead of using a simple string.

Related