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.
What’s the difference between this and the other question?
– Sergio
The file is added in head and when I run the code, the elements are not created. told to add in the body thin
– Marcelos Dourado
That’s right. No
<head>
you’re trying to create an element in<body>
when it has not yet been read.– Sam
I understand that is duplicate yes. Marceos, see the comment I left in the other question, below the answer of Sergio.
– bfavaretto
thanks for the information, I’m new here, I’m more aware now
– Marcelos Dourado