Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@SteveBenner
Last active December 17, 2015 01:29
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 SteveBenner/5528561 to your computer and use it in GitHub Desktop.
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
/**
* 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 + '>'));
}
@SteveBenner
Copy link
Author

I was only on my second programming project ever when I wrote this—I discovered templating shortly thereafter!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment