Create HTML elements using an external Javascript file

Asked

Viewed 22 times

0

I am new to Javascript and would like to create elements dynamically using external Javascript file. However I am unsuccessful.

Follow the code below:

function add (){

    var texto = document.createTextNode("teste");
    var p = document.createElement("h1");
    var ptexto = p.appendChild(texto);
    var body = document.getElementsByTagName("body")[0];
    body.appendChild(ptexto);

}

I can’t add the elements to the body of the document.

  • 1

    What’s the difference between this and the other question?

  • The file is added in head and when I run the code, the elements are not created. told to add in the body thin

  • 1

    That’s right. No <head> you’re trying to create an element in <body> when it has not yet been read.

  • I understand that is duplicate yes. Marceos, see the comment I left in the other question, below the answer of Sergio.

  • thanks for the information, I’m new here, I’m more aware now

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.