javascript append div to div code example

Example 1: js add more text to element

document.getElementById("p").textContent += " This is the text from javascript.";

<p id ="p">This is the text from HTML.</p>

Example 2: how to appendChild in the begin of the div javascript

var element = document.getElementById("div1");
element.insertBefore(para, element.firstChild);

Example 3: javascript add div to body with class

// correct me if im wrong
var MainDiv = document.createElement('div');
var ChildDiv1 = MainDiv.createElement('div');
ChildDiv1.className = 'contextmenu_contextMenu_3_a2Z contextmenu_ContextMenuFocusContainer_2thYU';
ChildDiv1.tabIndex = "0";
ChildDiv1.style = "visibility: visible; bottom: 56px; right: 68px;"
var ChildDiv2 = ChildDiv1.createElement('div');
ChildDiv1.className = 'contextmenu_contextMenuContents_1yyTu';
var DivButton1 = ChildDiv2.createElement('div');
DivButton1.className = 'contextmenu_contextMenuItem_CBC-y contextMenuItem';
DivButton1.value = "Upload an Image";
var DivButton2 = ChildDiv2.createElement('div');
DivButton2.className = 'contextmenu_contextMenuItem_CBC-y contextMenuItem';
DivButton2.value = "Share Your Trade Offer URL";
document.body.appendChild(MainDiv);

Example 4: JavaScript append HTML

let app = document.querySelector('#app');
app.append('append() Text Demo');

console.log(app.textContent);