create new html element javascript code example
Example 1: js create element
let myElm = document.createElement("p");
myElm.innerText = 'test';
myElm.style.color = 'red';
document.body.appendChild(myElm);
Example 2: create element javascript with id
var btn = document.createElement('div');
btn.setAttribute("id", "div1");
Example 3: how to add elements in javascript html
var tag = document.createElement("p");
var text = document.createTextNode("TEST TEXT");
tag.appendChild(text);
var element = document.getElementsByTagName("body")[0];
element.appendChild(tag);
Example 4: dom create element
var btn = document.createElement("BUTTON");
btn.innerHTML = "CLICK ME";
document.body.appendChild(btn);