0
votes

I have the following simple code where I try to create a div inside another div using appendChild.

let div1 = document.querySelector('div')
let div2 = document.createElement('DIV')
div1.appendChild(div2)
for (let x = 0; x < 100; x++) {
  div1.textContent += "Text "
  if (x <= 30) {
    div2.textContent += 'Yes '
  }
}

console.log(div1)
console.log(div2)
<div></div>
I am not sure why this code doesn't working. The div has already been created but just not appending to the div1.

Could anyone explain me what is wrong with the appendChild? I think there is no syntax problem with appendChild

Thanks for any responds!