Uncaught TypeError : element.appendChild is not a function

I am trying to implement infinite scroll on a page by appending some text in the body, but it's not working . I have seen almost all question regarding this but nothing seems satisfying , any help will be highly appreciated . Thank You :-)

Error in console

Uncaught TypeError: element.appendChild is not a function
at myscroll (<anonymous>:15:17)
at <anonymous>:22:9

index.html

<!DOCTYPE html>
  <html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Hello World! Site Title</title>
  </head>
<body>
  <h1>Hello World!</h1>
  <div class ="container">
    <h1> some random text to test infinite scroll  </h1>
    <h1> some random text to test infinite scroll  </h1>
    <h1> some random text to test infinite scroll </h1>
    <h1> some random text to test infinite scroll  </h1>
    <h1> some random text to test infinite scroll  </h1>
    <h1> some random text to test infinite scroll  </h1>
    <h1> some random text to test infinite scroll  </h1>
    <h1> some random text to test infinite scroll  </h1>
    <h1> some random text to test infinite scroll </h1>
    <h1> some random text to test infinite scroll  </h1>
    <h1> some random text to test infinite scroll </h1>
    <h1> some random text to test infinite scroll  </h1>
    <h1> some random text to test infinite scroll  </h1>
    </div>
   </body>
 </html>

JS file

const element = document.getElementsByClassName("container");

function myscroll(sum = 400){
    let i =0 ;
    let heading = document.createElement('h2');
    let text = document.createTextNode("Text Added to the body from js");
    let h=  heading.appendChild(text);

    const add = document.createElement('div');
    while(i < sum ){
        element.appendChild(h);
        i++;
    }
    
}

window.addEventListener('scroll' , ()=>{
    if(window.scrollY + window.innerHeight >= 
    document.documentElement.scrollHeight){
        myscroll();
    }
})


Read more here: https://stackoverflow.com/questions/68487484/uncaught-typeerror-element-appendchild-is-not-a-function

Content Attribution

This content was originally published by Abhijeet kumar at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: