-
-
Save levidurfee/7f6848b30b96d3fb97481c3a704a82cf to your computer and use it in GitHub Desktop.
Building a JavaScript Framework
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
function build(elementType, attributes, children, events) { | |
var element = document.createElement(elementType); | |
if(typeof attributes == "object") { | |
// Get the keys of the object | |
let keys = Object.keys(attributes); | |
for(let i=0; i<keys.length; i++) { | |
// Set each attribute using the keys we got from the object | |
element.setAttribute(keys[i], attributes[keys[i]]); | |
} | |
} | |
if(children) { | |
element.appendChild(children); | |
} | |
if(eventListener) { | |
// Get the keys from the object | |
let events = Object.keys(eventListener); | |
for(let x=0; x<events.length; x++) { | |
// Add each eventListener using the keys we got from the object | |
element.addEventListener(events[x], eventListener[events[x]]); | |
} | |
} | |
return element; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment