Last active
December 17, 2015 01:29
-
-
Save SteveBenner/5528561 to your computer and use it in GitHub Desktop.
This function constructs an HTML element and returns it, wrapped in a jQuery object
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
/** | |
* Constructs and returns an HTML element wrapped in a jQuery object | |
* | |
* @param {String} tag Name of the HTML element | |
* @param {Object} attr Map of attribute key/value pairs for the HTML element | |
* @param {String} content Content to be directly inserted inside HTML tags | |
*/ | |
function htmlElem(tag, attr, content) { | |
tag = tag || 'div'; attr = attr || ''; content = content || ''; // optional parameters | |
var htmlString = '<' + tag | |
for (var a in attr) { | |
if (attr[a] !== '') htmlString += (' ' + a + '="' + attr[a] + '"'); | |
} | |
return $(htmlString += ('>' + content + '</' + tag + '>')); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I was only on my second programming project ever when I wrote this—I discovered templating shortly thereafter!