js append element 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: .append js
let parent = document.createElement("div")
parent.append("Some text")
console.log(parent.textContent)
Example 4: how to appendChild in the begin of the div javascript
var element = document.getElementById("div1");
element.insertBefore(para, element.firstChild);
Example 5: how to append in javascript
var list=[1, 2, 3, 4, 5];
list.push(6);