Skip to content

Instantly share code, notes, and snippets.

@levidurfee levidurfee/index.js Secret
Last active Oct 23, 2018

Embed
What would you like to do?
Building a JavaScript Framework
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
You can’t perform that action at this time.