Last active
July 4, 2016 17:54
-
-
Save chrisgalard/540ba91317c29440e47577f2fb1f9bbd to your computer and use it in GitHub Desktop.
A method to easily append a child with attributes and content to an element
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Node.prototype.append = function(element, content, attribs) { | |
if (typeof element === 'string') { | |
element = document.createElement(element); | |
element.innerHTML = content ? content : ''; | |
} | |
for (var attrib in attribs) { | |
if (attribs.hasOwnProperty(attrib)) { | |
element.setAttribute(attrib, attribs[attrib]); | |
} | |
} | |
return this.appendChild(element); | |
} | |
// Usage | |
var parent = document.querySelector('#parent'); | |
parent.append('a', 'This is the content', { | |
class: 'child-a', | |
href:'http://nerdskills.io', | |
target: '_blank' | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment