2

i want to dynamically create 5 tr each having 3 tds with a loop, note: inside each i am filling dynamic html which has dynamic values that are filled from an array

<tr>
<td><td><td>
</tr>

<tr>
<td><td><td>
</tr>

<tr>
<td><td><td>
</tr>

<tr>
<td><td><td>
</tr>

<tr>
<td><td><td>
</tr>

what should be inside the td

 $('#output').append(

              + '<td>'
              + '<ul class="listing" style="margin:0 8px 0 0;width:220px;">'
              + '<li class="item">'
              + '<dl>'
              + '<dt><a href="' + pathname + value.SLink + '" >' + value.CName.replace("(Or Similar)", "") + '</a>' + '</dt>'
              + '<dd>fr £' + parseFloat(value.CPrice).toFixed(0) + '</dd>'
              + '<dd class="last">' + value.CType + ', ' + value.CSize + '<br>'
              + ' Ad: ' + value.Ad
              + ', Chi: ' + value.Chi + '<br>'
              + ' Lu: ' + value.Big + ' Big, '
              + value.Lu + ' Small'                 
              + '</dd>'
              + '</dl>'
              + '</li>'
              + '</ul>'
              + '</td>'

                     );
1
  • I don't see 4 DIV's among the contents of the TD.. ? Commented Nov 22, 2011 at 12:57

3 Answers 3

5

Simple nested loop:

var content;
for (var i = 1; i<= 5; $i++){
    content += '<tr>';

    for (var j = 1; j<= 3; j++){
        content+= '<td><ul class="listing" style="margin:0 8px 0 0;width:220px;">'
          + '<li class="item">'
          + '<dl>'
          + '<dt><a href="' + pathname + value.SLink + '" >' + value.CName.replace("(Or Similar)", "") + '</a>' + '</dt>'
          + '<dd>fr £' + parseFloat(value.CPrice).toFixed(0) + '</dd>'
          + '<dd class="last">' + value.CType + ', ' + value.CSize + '<br>'
          + ' Ad: ' + value.Ad
          + ', Chi: ' + value.Chi + '<br>'
          + ' Lu: ' + value.Big + ' Big, '
          + value.Lu + ' Small'                 
          + '</dd>'
          + '</dl>'
          + '</li>'
          + '</ul>'
          + '</td>';
    }
    content += '</tr>';
}
$("body").append(content);
Sign up to request clarification or add additional context in comments.

4 Comments

That will not work. jQuery will automatically close your first <tr> right after it was created and will add all <td>s directly to the body
$("body").append('<td></td>'); how to add dynamic div inside the td tag like div
@Al25 concatenation. '<td>'+dynamicDiv+'</td>'
@Martin uplovoted, but change the last line to $("body").append(content)
1
$(document).ready(function(){
    table = $('<table>');
    for (var i =0; i<5; i++){
      var tr = $('<tr>');
      for (var j =0; j<3;j++){
        var td = $('<td>');
        $(td).append('<span>All your html code for TD goes here</span>');
        $(td).appendTo(tr);
      }
      $(tr).appendTo(table);
    }
    $(table).appendTo('body');
});

jsFiddle

Comments

0
function consTrTd (elementId, trCount, tdCount){
    for(var i = 0; i < trCount; i++){

     var ttr = $("<tr>").appendTo("#"+elementId);
        for(var j = 0; j < tdCount; j++ )
             ttr.append("<td>");
    }
}

consTrTd(1,5,3);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.