0

I have now read this thread several times: Add table row in jQuery .... and still don't understand why I can't append to a row that I just created:

var row = $('#sitesTable').append('<tr/>');
        row.append($('<td align="left"/>').text(k));
        row.append($('<td align="center"/>').text(v.length));

This simply creates an empty tr tag without appending to it:

<table class="sitesTable" id="sitesTable">
<tr></tr>
<th align="left">Site</th>
<th align="right">Total</th>
...

I appreciate any assistance, as this is starting to drive me nuts.

0

1 Answer 1

2

Because var row = $('#sitesTable').append('<tr/>'); returns a reference to the table not the newly created row

you can use

var row = $('<tr/>').appendTo('#sitesTable');

var row = $('<tr/>').appendTo('#sitesTable');
$('<td align="left"/>').text('col-1').appendTo(row);
$('<td align="center"/>').text('col-2').appendTo(row);
// I prefer the above format for readability
//row.append($('<td align="left"/>').text('col-1'));
//row.append($('<td align="center"/>').text('col-2'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<table class="sitesTable" id="sitesTable">
  <tr>
    <th align="left">Site</th>
    <th align="right">Total</th>
  </tr>
</table>

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

2 Comments

Also, this works: row = $("#sitesTable").find("tr").last(); Now I can append to the newly created row. Thanks.
@SilverVesi Since you can have the added row reference, I prefer to use that

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.