Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

230
Views
Why Insert before first child not working

If the element has children insert a new div "i-new-element". parent2 and parent3 get a new div.

child3-parent3 has children but doesn't get a new giant. Why?

How can I make it possible for children who have children to get a new div?

it should look like:

<div id="child3-parent3">
  <div id="i-new-element"></div>
  <div id="child3"></div>
  <div id="child4"></div>
</div>

var container = document.getElementById("container").querySelectorAll("#container > *");

container.forEach(function(div) {
  {
    if (div.hasChildNodes()) {
      let parentElement = div;
      let theFirstChild = parentElement.firstChild;
      let newElement = document.createElement("div")
      newElement.id = "i-new-eleemnt"
      parentElement.insertBefore(newElement, theFirstChild)
    }
  }
});
<div id="container">
  <div id="parent1"></div>
  <div id="parent2">
    <div id="child1"></div>
    <div id="child2"></div>
    <div id="child3-parent3">
      <div id="child3"></div>
      <div id="child4"></div>
    </div>
  </div>
  <div id="parent3">
    <div id="child5"></div>
    <div id="child6"></div>
  </div>
</div>

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Using

var container = document.getElementById("container");
var elements = container.querySelectorAll(":scope *");

instead of

var container = document.getElementById("container").querySelectorAll("#container > *");

should fix your problem.

var container = document.getElementById("container");
   var elements = container.querySelectorAll(":scope *");

elements.forEach(function(div){
  {
    if (div.hasChildNodes()) {
      let parentElement = div;
      let theFirstChild = parentElement.firstChild;
      let newElement = document.createElement("div")
      newElement.id = "i-new-eleemnt"
      parentElement.insertBefore(newElement, theFirstChild)
    }
  }
});
<div id="container">
  <div id="parent1"></div>
  <div id="parent2">
    <div id="child1"></div>
    <div id="child2"></div>
    <div id="child3-parent3">
      <div id="child3"></div>
      <div id="child4"></div>
    </div>
  </div>
  <div id="parent3">
    <div id="child5"></div>
    <div id="child6"></div>
  </div>
</div>


Edit: Further information about the :scope CSS pseudo-class can be found here and here.

about 4 years ago · Juan Pablo Isaza Report

0

I believe your problem is the following, even it is not described well in your sample HTML:

<div id="parent1"></div> per instance is actually not an empty node but contains some text i.e. <div id="parent1">some text here</div>.

You might believe or not, but node.hasChildNodes() will count any text as a child node (text node, nodeType = 3), so it will always return true is any text is present.

To avoid that, you can filter the text nodes first or just use this workaround:

Replace this line:

if (div.hasChildNodes()) {

with that line:

if (div.children.length) {

children property is not counting text nodes.

That's all you have to do, I believe.

var container = document.querySelectorAll("#container > *");

container.forEach(function(div) {
  {
    if (div.children.length) {
      let parentElement = div;
      let theFirstChild = parentElement.children[0];
      let newElement = document.createElement("div")
      newElement.id = "i-new-eleemnt";
      newElement.innerHTML = 'i-new-eleemnt'
      parentElement.insertBefore(newElement, theFirstChild);
    }
  }
});
<div id="container">
  <div id="parent1">parent1</div>
  <div id="parent2">parent2
    <div id="child1">child1</div>
    <div id="child2">child2</div>
    <div id="child3-parent3">child3-parent3
      <div id="child3">child3</div>
      <div id="child4">child4</div>
    </div>
  </div>
  <div id="parent3">parent3
    <div id="child5">child5</div>
    <div id="child6">child6</div>
  </div>
</div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!