Skip to content

Instantly share code, notes, and snippets.

@kristiannissen
Created May 19, 2016 13:38
Show Gist options
  • Select an option

  • Save kristiannissen/0af4885ee066e3f3fa77829c1660bb35 to your computer and use it in GitHub Desktop.

Select an option

Save kristiannissen/0af4885ee066e3f3fa77829c1660bb35 to your computer and use it in GitHub Desktop.
Building tags
function t(nodeName) {
var elm = document.createElement(nodeName);
if (typeof arguments[1] === 'string' || typeof arguments[2] === 'string') {
elm.innerHTML = arguments[2] || arguments[1];
}
if (typeof arguments[1] === 'object' && !Array.isArray(arguments[1])) {
// Add attributes
var attr = arguments[1];
Object.keys(attr).map(function(key) {
elm.setAttribute(key, attr[key]);
});
}
if (Array.isArray(arguments[1]) || Array.isArray(arguments[2])) {
var args = arguments[2] || arguments[1];
Object.keys(args).forEach(function(key) {
elm.appendChild(args[key]);
});
}
return elm;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment