var parent = document.querySelector('.parent');
var textNode = document.createTextNode('Static Child 2');
var headingNode = document.createElement('h1');
var divNode = document.createElement('div');
divNode.classList.add('child');
append(headingNode, textNode);
append(divNode, headingNode);
append(parent, divNode);
/**
* after
* @param {Element} element
* @param {String|Node|NodeList|Array<Node>} content
*/
function after(element, content) {
if (isString(content)) {
return element.insertAdjacentHTML('afterEnd', content);
} else {
// when the second parameter of 'insertBefore' is 'null' the content is
// appended to the end of the 'parentNode'.
return element.parentNode.insertBefore(normalizeContent(content), element.nextSibling);
}
}
/**
* append
* @param {Element} element
* @param {String|Node|NodeList|Array<Node>} content
*/
function append(element, content) {
if (isString(content)) {
return element.insertAdjacentHTML('beforeEnd', content);
} else {
return element.appendChild(normalizeContent(content));
}
}
/**
* before
* @param {Element} element
* @param {String|Node|NodeList|Array<Node>} content
*/
function before(element, content) {
if (isString(content)) {
return element.insertAdjacentHTML('beforeBegin', content);
} else {
return element.parentNode.insertBefore(normalizeContent(content), element);
}
}
/**
* prepend
* @param {Element} element
* @param {String|Node|NodeList|Array<Node>} content
*/
function prepend(element, content) {
if (isString(content)) {
return element.insertAdjacentHTML('afterBegin', content);
} else {
return element.insertBefore(normalizeContent(content), element.firstChild);
}
}
/**
* Determine if the value is a string
* @return {Boolean}
*/
function isString(value) {
return typeof value == 'string';
}
/**
* Determine if the object is a DOM Node
* @return {Boolean}
*/
function isNode(o) {
return (
typeof Node == 'object' ?
o instanceof...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.