mizner
4/30/2017 - 12:04 AM

Element Creation Example in JavaScript

Element Creation Example in JavaScript

const app = document.getElementById('app');

const createElement = (el, text, newEl) => {
    let textNode = document.createTextNode(text),
        newElement = document.createElement(newEl),
        module = newElement.appendChild(textNode);
    el.appendChild(module)
};

// We're using a document fragment to group things up
const docFrag = document.createDocumentFragment();

createElement(docFrag, 'Hello Buddy', 'H1');
createElement(docFrag, 'Hello Buddy', 'H1');
createElement(docFrag, 'Hello Buddy', 'H1');
createElement(docFrag, 'Hello Buddy', 'H1');

app.appendChild(docFrag);