appendchild jquery code example
Example 1: jquery append
$("p").append(" <b>Appended text</b>.");
Example 2: jquery append div
$('#box').append(
$('<div/>')
.attr("id", "newDiv1")
.addClass("newDiv purple bloated")
.append("<span/>")
.text("hello world")
);
Example 3: appendchild javascript
const parent = document.createElement('div');
const child = document.createElement('p');
// Appending Node Objects
parent.append(child) // Works fine
parent.appendChild(child) // Works fine
// Appending DOMStrings
parent.append('Hello world') // Works fine
parent.appendChild('Hello world') // Throws error
Example 4: how to append in javascript
var list=[1, 2, 3, 4, 5];
list.push(6);
// .push allows you to add a value to the end of a list
Example 5: for each append to document
var person = {
firstName: "john",
lastName: "doe",
age: 45,
placeOfBirth: "somewhere"
}
for(var key in person) {
var p = document.createElement("p");
p.innerHTML = person[key];
document.body.appendChild(p)
}