Skip to content
Advertisement

Insert tag in loop javascript

I want to insert the p tag after each rect tag but unfortunately when I used appendChild the p tag is insert into the rect tag and not after:

JS code:

$("#id_svg .nv-series-0 rect").each(function(index){ 

var line = document.createElement("p");
  line.className = "line";
  line.innerHTML = '|';


this.appendChild(line);

)}

I had as a result: ** html Code:**

<svg id="id_svg">
    <g class="nv-series-0">
        <rect class="nv-bar positive">
            <p class="line">|</p>
        </rect>
    </g>
</svg>

Advertisement

Answer

You can use Element.insertAdjacentElement instead of appendChild (which inserts it as a child inside).

this.insertAdjacentElement('afterend', line);
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement