Skip to content

Instantly share code, notes, and snippets.

@levidurfee
Last active October 23, 2018 00:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save levidurfee/7f6848b30b96d3fb97481c3a704a82cf to your computer and use it in GitHub Desktop.
Save levidurfee/7f6848b30b96d3fb97481c3a704a82cf to your computer and use it in GitHub Desktop.
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