Profile

Join date: Sep 13, 2022

About
0 Like Received
0 Comment Received
0 Best Answer

How to CreateElement in JavaScript?

The JavaScript function is all you need to create HTML element. With this function, you can define a unique Web page template and add elements dynamically. But from the beginning of your next HTML5 project, you will have some advantages over traditional ones:

js create element

let myElm = document.createElement("p");// Create a new element


myElm.innerText = 'test';// Change the text of the element

myElm.style.color = 'red';// Change the text color of the element


document.body.appendChild(myElm);// Add the object to the DOM


Source: Codeprozone.com

create element javascript with id

//adding 'p' tag to body


var tag = document.createElement("p"); // <p></p>

var text = document.createTextNode("TEST TEXT");

tag.appendChild(text); // <p>TEST TEXT</p>

var element = document.getElementsByTagName("body")[0];

element.appendChild(tag); // <body> <p>TEST TEXT</p> </body>

 

abakhsh

More actions