public
Last active

  • Download Gist
buildHTML utility
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
// 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" />

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.