-
-
Save chalist/3531ce0ad9668d018d6deb58f28faf20 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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