Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@1Marc
Created July 7, 2010 07:34
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save 1Marc/466432 to your computer and use it in GitHub Desktop.
Save 1Marc/466432 to your computer and use it in GitHub Desktop.
// my little html string builder
buildHTML = function(tag, html, attrs) {
// you can skip html param
if (typeof(html) != 'string') {
attrs = html;
html = null;
}
var h = '<' + tag;
for (attr in attrs) {
if (attr == 'selected' && attrs[attr] == false) continue;
if (attr == 'disabled' && attrs[attr] == false) continue;
if (attr == 'checked' && attrs[attr] == false) continue;
h += ' ' + attr + '="' + attrs[attr] + '"';
}
return h += html ? ">" + html + "</" + tag + ">" : "/>";
}
buildHTML("a", "Marc Grabanski", {
id: "mylink",
href: "http://marcgrabanski.com"
});
// outputs: <a id="mylink" href="http://marcgrabanski.com">Marc Grabanski</a>
// or leave out the html
buildHTML("input", {
id: "myinput",
type: "text",
value: "myvalue"
});
// outputs: <input id="myinput" type="text" value="myvalue" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment