Created
June 5, 2012 22:36
-
-
Save devinrhode2/2878569 to your computer and use it in GitHub Desktop.
A better createElement function, that takes a map of
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
//turns this: | |
var button = document.createElement('img'); | |
button.src = 'images/button.png'; | |
button.id = 'button'; | |
button.className = 'button'; | |
//to this: | |
var button = $.createElement('img', { | |
src: 'images/button.png', | |
id: 'button', | |
className: 'thatOneButton' | |
}); | |
//with this: | |
$.createElement = function createElement(element, props, attributes) { | |
var element = document.createElement(element); | |
if (typeof props !== 'undefined') { | |
for (var prop in props) { | |
element[prop] = props[prop]; | |
} | |
if (typeof attributes !== 'undefined') { | |
for (var attr in attributes) { | |
element.setAttribute(attr, attributes[attr]); | |
} | |
} | |
} | |
return element; | |
}; | |
/*And it doesn't break the current document.createElement function! | |
If the 2nd param is gone, then it just returns the normal element!*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment