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 | |
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