Skip to content

Instantly share code, notes, and snippets.

@0xdevalias
Forked from 1Marc/buildHTML utility
Last active October 15, 2024 07:35
Show Gist options
  • Select an option

  • Save 0xdevalias/11339206 to your computer and use it in GitHub Desktop.

Select an option

Save 0xdevalias/11339206 to your computer and use it in GitHub Desktop.

Revisions

  1. 0xdevalias revised this gist Apr 27, 2014. 1 changed file with 15 additions and 18 deletions.
    33 changes: 15 additions & 18 deletions buildHTML utility
    Original file line number Diff line number Diff line change
    @@ -1,25 +1,22 @@
    // 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 - Helper method to construct html tags easily */
    var buildHtml = function(tag, attrs, innerHtml) {
    var h = '<' + tag;
    for (var attr in attrs) {
    if(attrs[attr] === false) {
    continue;
    }
    h += ' ' + attr + '="' + attrs[attr] + '"';
    }
    return h += innerHtml ? '>' + innerHtml + '</' + tag + '>' : '/>';
    }

    buildHTML("a", "Marc Grabanski", {
    buildHTML("a", {
    id: "mylink",
    href: "http://marcgrabanski.com"
    });
    // outputs: <a id="mylink" href="http://marcgrabanski.com">Marc Grabanski</a>
    href: "http://devalias.net/"
    }, "Glenn devalias Grant");
    // outputs: <a id="mylink" href="http://devalias.net">Glenn devalias Grant</a>

    // or leave out the html
    // or leave out the innerHtml
    buildHTML("input", {
    id: "myinput",
    type: "text",
  2. @1Marc 1Marc revised this gist Jul 8, 2010. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions buildHTML utility
    Original file line number Diff line number Diff line change
    @@ -7,9 +7,7 @@ buildHTML = function(tag, html, attrs) {
    }
    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;
    if(attrs[attr] === false) continue;
    h += ' ' + attr + '="' + attrs[attr] + '"';
    }
    return h += html ? ">" + html + "</" + tag + ">" : "/>";
  3. @1Marc 1Marc revised this gist Jul 7, 2010. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion buildHTML utility
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,13 @@
    // my little html string builder
    function buildHTML(tag, html, attrs) {
    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] + '"';
  4. @1Marc 1Marc revised this gist Jul 7, 2010. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion buildHTML utility
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,11 @@ function buildHTML(tag, html, attrs) {
    html = null;
    }
    var h = '<' + tag;
    for (attr in attrs) h += ' ' + attr + '="' + attrs[attr] + '"';
    for (attr in attrs) {
    if (attr == 'disabled' && attrs[attr] == false) continue;
    if (attr == 'checked' && attrs[attr] == false) continue;
    h += ' ' + attr + '="' + attrs[attr] + '"';
    }
    return h += html ? ">" + html + "</" + tag + ">" : "/>";
    }

  5. @1Marc 1Marc revised this gist Jul 7, 2010. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion buildHTML utility
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@ function buildHTML(tag, html, attrs) {
    }
    var h = '<' + tag;
    for (attr in attrs) h += ' ' + attr + '="' + attrs[attr] + '"';
    return h + html ? ">" + html + "</" + tag + ">" : "/>";
    return h += html ? ">" + html + "</" + tag + ">" : "/>";
    }

    buildHTML("a", "Marc Grabanski", {
  6. @1Marc 1Marc revised this gist Jul 7, 2010. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion buildHTML utility
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    // my little html string builder
    function buildHTML(tag, html, attrs) {
    if (typeof(html) != 'string') { // you can skip html param
    // you can skip html param
    if (typeof(html) != 'string') {
    attrs = html;
    html = null;
    }
  7. @1Marc 1Marc created this gist Jul 7, 2010.
    24 changes: 24 additions & 0 deletions buildHTML utility
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    // my little html string builder
    function buildHTML(tag, html, attrs) {
    if (typeof(html) != 'string') { // you can skip html param
    attrs = html;
    html = null;
    }
    var h = '<' + tag;
    for (attr in attrs) 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" />