Skip to content

Instantly share code, notes, and snippets.

@1Marc
Created July 7, 2010 07:34
Show Gist options
  • 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(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