javascript append html string code example

Example 1: 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 2: jquery append

$("p").append(" <b>Appended text</b>.");

Example 3: 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 4: jquery append element to body

$( ".inner" ).append( "<p>Test</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: 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