foreachparent-childcreateelementinsertafterinsertadjacenthtml

Create a div for each parent?


I need to wrap all the childrens of the parent with a div. In the example below I managed to put a div tag after each parent but how do I put an end /div tag at the end of the chidrens.

I tried with document.createElement, and now with insertAdjacentHTML ..., what is the best way to fix this, or the best way to encode it?

     var divs = document.getElementById("container").querySelectorAll('.activ-window *'); 
     var elements = "";
     divs.forEach(function(elem){
         if (elem.firstChild) {
                    
          elements += elem.insertAdjacentHTML("afterbegin", '<div id="afterParent" style="display:block;">');
                      elem.insertAdjacentHTML("afterend", '</div>');
         }
     });
     document.getElementById("demo").innerHTML = elements;
<div id="container" class="activ-window">

    <div id="parent1">
        <div id="child" ></div>
        <div id="child" ></div>
    </div>
    <div id="parent2">
        <div id="child" ></div>
        <div id="paren3" >
             <div id="child" ></div>
             <div id="child" ></div>
             <div id="child" ></div>
        </div>
    </div>

</div>

It should look like:


<div id="container" class="activ-window">

    <div id="parent1">
        <div id="afterParent" style="display:block;">
        <div id="child" ></div>
        <div id="child" ></div>
        </div>
    </div>
    <div id="parent2">
        <div id="afterParent" style="display:block;">
        <div id="child" ></div>
        </div>
        <div id="paren3" >
        <div id="afterParent" style="display:block;">
             <div id="child" ></div>
             <div id="child" ></div>
             <div id="child" ></div>
        </div>
        </div>
    </div>

</div>

Edit for Nick: This is with text(see Parent1):

    <div id="parent1">Parent1
        <div id="child" >child1</div>
        <div id="child" >child1</div>
    </div>

I get it with your code

    <div id="parent1">
        <div id="afterParent" style="display:block;">
        "Parent1"
        <div id="child" >Child1</div>
        <div id="child" >Child1</div>
        </div>
    </div>

It should look:

    <div id="parent1">Parent1
        <div id="afterParent" style="display:block;">
        <div id="child" >Child1</div>
        <div id="child" >Child1</div>
        </div>
    </div>

Solution

  • If you want to enclose all the divs beneath each parent in a new div, you can create a new div element, move all the non-text children to it, and then add that div as a child to the original parent:

    const container = document.getElementById("container");
    const parentDivs = container.querySelectorAll('.activ-window div[id^="parent"]');
    
    for (let parent of parentDivs) {
      // create a new div
      let after = document.createElement('div');
      after.id = 'after' + parent.id;
      after.style.display = 'block';
      // move the parent's non-text children to it
      let children = parent.childNodes;
      for (let i = 0; i < children.length; i++) {
        if (children[i].nodeType != Node.TEXT_NODE) {
          after.append(children[i]);
        }
      }
      // and append it to the parent
      parent.appendChild(after);
    }
    
    console.log(container.innerHTML);
    <div id="container" class="activ-window">
      <div id="parent1">
        Parent1
        <div id="child1"></div>
        <div id="child2"></div>
      </div>
      <div id="parent2">
        Parent2
        <div id="child3"></div>
        <div id="parent3">
          <div id="child4"></div>
          <div id="child5"></div>
          <div id="child6"></div>
        </div>
      </div>
    </div>