ajm
ajm

Reputation: 13213

How to append html to table after tr with id using jquery?

I have a html table with few rows in it. Now I want to append a new row i.e. tr. I am generating a html for this.

html = "<tr><td>some html</td></tr>";

Then I am using following code to append this html to table.

jQuery("#matchTable > tbody:last").append(html);

This is appending to the table after last tr. I want to append after certain tr with given id i.e. in between rows.

Please help !!!!

Upvotes: 3

Views: 15576

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388316

You need to use .after()

jQuery("#matchTable > tbody:last").after(html);

Demo: Fiddle

Update

<table id="matchTable">
    <tbody>
        <tr id="i1">
            <td>1</td>
        </tr>
        <tr id="i2">
            <td>2</td>
        </tr>
        <tr id="i3">
            <td>3</td>
        </tr>
    </tbody>
</table>

then

var html = "<tr><td>some html</td></tr>";
jQuery("#i2").after(html);

Demo: Fiddle

Upvotes: 8

Related Questions