'; html...">

JQuery: html generation

Is there a better way to do the following?

var html;
var i;
for (i=0; i < 4; i++) {
    html += '<div class="row">';
    html += '<span class="label">Dining Style:</span>';
    html += '<span class="control">';

    var j;
    for (j=0; j < 3; j++){
        html += '<div class="attribBox">';
            html += '<ul>';
                html += '<li><input type="checkbox" /> item 1</li>';
            html += '</ul>';
        html += '</div>';
    }
    html += '</span>';
    html += '<div class="clear"></div>';
}

$("#content").html(html);
+5
source share
4 answers

Instead of creating html, you can use the jquery, functions available in version 1.4 + to make the code much cleaner. Here is an example:

$("#content").append($("<div/>", {class : 'row'}).append($("<span/>", {class:'label', text : 'Dining Style'})))

The above code will generate:

<div class="row"><span class="label">Dining Style</span></div>

You still need to create a loop to create three children, but it is much cleaner.

+5
source
+1

All Articles