Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
// my little html string builder
buildHTML = function(tag, html, attrs) {
// you can skip html param
var h = document.createElement(tag);
var attrs = attrs || {};
if (arguments.length == 2 && !((html) instanceof HTMLElement) && typeof(html) == "object") {
attrs = html;
html = null;
}
for (attr in attrs) {
if(attrs[attr] === false) continue;
h.setAttribute(attr, attrs[attr]);
}
if ( html instanceof HTMLElement ){
h.appendChild(html)
} else if ( typeof(html) == "string" ) {
h.appendChild(document.createTextNode(html));
}
return h;
};
buildHTML("a", "Marc Grabanski", {
id: "mylink",
href: "http://marcgrabanski.com"
});
// outputs: <a id="mylink" href="http://marcgrabanski.com">Marc Grabanski</a>
buildHTML("a", buildHTML("strong", "Marc Grabanski"), {
id: "mylink",
href: "http://marcgrabanski.com"
});
// outputs: <a id="mylink" href="http://marcgrabanski.com"><strong>Marc Grabanski</strong></a>
buildHTML("a", {
id: "mylink",
href: "http://marcgrabanski.com"
});
// outputs: <a id="mylink" href="http://marcgrabanski.com"></a>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.