Skip to content

Instantly share code, notes, and snippets.

@anurlybayev
Forked from 1Marc/buildHTML utility
Created July 13, 2011 22:26
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 anurlybayev/1081477 to your computer and use it in GitHub Desktop.
Save anurlybayev/1081477 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