2
jq('#table1') 
  .append(jq('<tr>') 
    .append(jq('<td>') 
      .append('data1' 
      ) 
    ) 
  ); 

I want to append more than one <td> to my current <tr>, each of these <td> has different format, so I can not just use a for loop to create bunch of <td>. The ( ) in this case is very tricky, anybody got an idea how this might work?

By each of these <td> has different format, I mean that I need to add $ to some of the cells that representing money, etc.

3
  • What do you mean each has a different format? Commented Mar 15, 2012 at 22:14
  • You'll have to be more specific about what you're trying to do if you want a good answer. Commented Mar 15, 2012 at 22:20
  • 1
    Hiya - this might help - stackoverflow.com/questions/1075415/… , cheers! Commented Mar 15, 2012 at 22:22

4 Answers 4

3
jq('#table1').append(
    jq('<tr>')
        .append(jq('<td>').append('data1')) 
        .append(jq('<td>').append('data2')) 
        .append(jq('<td>').append('data3')) 
);

EDIT: Corrected to append tds, not data in tds

Sign up to request clarification or add additional context in comments.

2 Comments

In ur case, I would be adding data to the same <td> cell
My apologies, I misread your goal. I corrected the above to reflect what you want.
1

Create the markup first and append everything at last for best performance:

var tds = [
    '<td>...</td>',
    '<td>...</td>',
    '<td>...</td>'
];

$('#table').append('<tr>' + tds.join('') + '</tr>');

Comments

1
jq("#table1").append("<tr></tr>"); 

then;

jq("#table1 tr:last").append("<td></td>");
jq("#table1 tr:last").append("<td></td>");
jq("#table1 tr:last").append("<td></td>");
jq("#table1 tr:last").append("<td></td>");

will append 4 td's to your last added tr.

1 Comment

and if you want data inside the td's, you'd just write "<td>"+data+"</td>" inside the brackets.
1
  jq('#table1') 
  .append(jq('<tr>') 
    .append(jq('<td>').append('data1')) 
    .append(jq('<td>').append('data1')) 
    .append(jq('<td>').append('data2')) 
    .append(jq('<td>').append('data3'))     
  ); 

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.