Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

append HTML elements in JavaScript

let div = document.createElement("div")
div.append("Some text")

console.log(div.textContent) // "Some text"
Comment

javascript html append

// The classic createElement
// -------------------------
// create a paragraph element using document.createElement
const elem = document.createElement(`p`);
elem.id = `myBrandnewDiv1`;

// put in some text
elem.appendChild(document.createTextNode(`My brand new div #1`));

// append some html (for demo, preferrably don't use innerHTML)
elem.innerHTML += ` => created using 
  <code>document.createElement</code>`;

// append a new paragraph within #myBrandNewDiv1
const nested = elem.appendChild(document.createElement(`p`));
nested.classList.add(`nested`);
// add some text to that
nested.textContent = `I am nested!`;
// the elements are still in memory, now add the 
// whole enchillada to the document
document.body.appendChild(elem);

// insertAdjacentHTML
// ------------------
// nest an element within the nested div
nested.insertAdjacentHTML(`afterbegin`, 
  `<div id="nestedWithin#nested">
    This text will appear <i>above</i> the text of 
    my parent, that being div#nested.
    Someone had the nerve to insert me using 
    <code>insertAdjacentHTML</code>
   </div>`);

// Object.assign
// -------------
// Use Object.assign to create an element and
// assign properties/html to it in one go
const newElem = Object.assign(
  document.createElement(`div`), 
  { id: `myBrandnewDiv2`, 
    innerHTML: `div#myBrandnewDiv2 signing in. 
      I was <i>assigned</i> using <code>Object.assign</code>&hellip;`});
document.body.appendChild(newElem);

// insertAdjacentElement combined with Object.assign
// -------------------------------------------------
// use the above technique combined with insertAdjacentElement
newElem.insertAdjacentElement(
  `beforeend`,
    Object.assign(document.createElement(`span`), 
      { id: `myBrandnewnested2_nested`, 
        innerHTML: `<br>Me too! And appended I was 
          with <code>insertAdjacentElement</code>` })
);

// createDocumentFragment
// ----------------------
// Use a document fragment to create/inject html
const fragment = document.createDocumentFragment();
const mdnLnk = `https://developer.mozilla.org/en-US/` +
    `docs/Web/API/Document/createDocumentFragment`;
fragment.appendChild(
  Object.assign(
    document.createElement(`p`), 
    {innerHTML: `Regards from <code>createDocumentFragment</code> 
    (see <a href="${mdnLnk}">MDN</a>)`})
);
document.querySelector(`#myBrandnewDiv2`).appendChild(fragment);

// Create, but don't inject
// ------------------------
const virtual = Object.assign(
      document.createElement(`p`), 
      { innerHTML: `       
        <a href="#id1">id1</a>
        <div id="id2">Hi!</div>
        <p id="id3">Hi 2!</p>`,
        classList: [`xyz`], } );

const prepareHtml4Reporting = html => 
  html.replace(/</g, `&lt;`)
    .replace(/
s+/g, `
`)
    .replace(/

/g, `
`);
    
document.body.insertAdjacentHTML(
  `beforeend`,
  `<h3>html only</h3><pre>${
     prepareHtml4Reporting(virtual.innerHTML)}</pre>`);
Comment

JavaScript append HTML


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

console.log(app.textContent);
Comment

append javascript example

const formDataId = document.getElementById("userForm");
const formData = new FormData(formDataId);
formData.append("isAdmin", 1);
Comment

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 
Comment

PREVIOUS NEXT
Code Example
Javascript :: repeat an element in array in js 
Javascript :: react chart js 2 
Javascript :: mongodb mongoose push into nested array 
Javascript :: js then 
Javascript :: regular expression characters 
Javascript :: javascript object without undefined values 
Javascript :: find a word in string javascript 
Javascript :: js add animation to element 
Javascript :: how to poll efficiently in javascript 
Javascript :: sanitizer content nodejs 
Javascript :: ngrok live port vue js 
Javascript :: How to do a timer angular 
Javascript :: plotly react 
Javascript :: descending order in objects in js 
Javascript :: How to get latitude and longitude from address in angular 6 
Javascript :: js array from 
Javascript :: dropify use 
Javascript :: express server sockjs 
Javascript :: how to create package.json file in vs code 
Javascript :: enzyme-adapter-react-17 
Javascript :: fetch method in javascript 
Javascript :: mongodb mongoose aggregate two collections using lookup & format the result set. 
Javascript :: javascript slice array 
Javascript :: react native vector icons not working 
Javascript :: http node 
Javascript :: discord js lockdown command 
Javascript :: reverse array without using another array js 
Javascript :: get user country code javascript 
Javascript :: date javascript format 
Javascript :: npm fs 
ADD CONTENT
Topic
Content
Source link
Name
4+9 =