javascript add div to div code example
Example 1: add element to body javascript
var elem = document.createElement('div');
elem.style.cssText = 'position:absolute;width:100%;height:100%;opacity:0.3;z-index:100;background:#000';
document.body.appendChild(elem);
Example 2: javascript append to paragraph
// In the JS script
var parElement = document.getElementById("myPar");
var textToAdd = document.createTextNode("Text to be added");
parElement.appendChild(textToAdd);
//In the HTML file
<p id="myPar"></p>
Example 3: javascript add div
<div id="new">
<p id="p1">Tutorix</p>
<p id="p2">Tutorialspoint</p>
</div>
<script>
var tag = document.createElement("p");
var text = document.createTextNode("Tutorix is the best e-learning platform");
tag.appendChild(text);
var element = document.getElementById("new");
element.appendChild(tag);
</script>
Example 4: 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 5: how to appendChild in the begin of the div javascript
var element = document.getElementById("div1");
element.insertBefore(para, element.firstChild);
Example 6: 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);