Answers for "javascript generate html elements"

12

js create element

var newDiv = document.createElement("div");
document.body.appendChild(newDiv);
Posted by: Guest on April-04-2020
0

Inserting HTML elements with JavaScript

function create(htmlStr) {
    var frag = document.createDocumentFragment(),
        temp = document.createElement('div');
    temp.innerHTML = htmlStr;
    while (temp.firstChild) {
        frag.appendChild(temp.firstChild);
    }
    return frag;
}

var fragment = create('<div>Hello!</div><p>...</p>');
// You can use native DOM methods to insert the fragment:
document.body.insertBefore(fragment, document.body.childNodes[0]);
Posted by: Guest on February-24-2020

Code answers related to "javascript generate html elements"

Code answers related to "Javascript"

Browse Popular Code Answers by Language