-
-
Save kristiannissen/0af4885ee066e3f3fa77829c1660bb35 to your computer and use it in GitHub Desktop.
Building tags
This file contains hidden or 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
| 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