how to append dom element in javascript code example
Example 1: create and append element in javascript
var node = document.createElement("LI");
var textnode = document.createTextNode("Water");
node.appendChild(textnode);
document.getElementById("myList").appendChild(node);
Example 2: appendchild javascript
function createMenuItem(name) {
let li = document.createElement('li');
li.textContent = name;
return li;
}
const menu = document.querySelector('#menu');
menu.appendChild(createMenuItem('Home'));
menu.appendChild(createMenuItem('Services'));
menu.appendChild(createMenuItem('About Us'));
Example 3: appendchild javascript
const parent = document.createElement('div');
const child = document.createElement('p');
parent.append(child)
parent.appendChild(child)
parent.append('Hello world')
parent.appendChild('Hello world')