Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

append row after first row in a html table

I have var row=<tr><td>val</td><td>val2</td></tr> and I tried this:

$("#mainTable tbody").append(row); 

but it appends to the end of the table.

Also I tried $("#mainTable tr:first").after().append(row);

But have not got a result yet.

Please, help me to understand.

like image 266
loviji Avatar asked Mar 16 '10 17:03

loviji


People also ask

How to add rows in table HTML?

The insertRow() method creates an empty <tr> element and adds it to a table. The insertRow() method inserts the new row(s) at the specified index in the table. Note: A <tr> element must contain one or more <th> or <td> elements.

How do you jump a row in HTML?

To add a line break to your HTML code, you use the <br> tag. The <br> tag does not have an end tag. You can also add additional lines between paragraphs by using the <br> tags.


2 Answers

Try this:

$("#mainTable tr:first").after(row); 

http://api.jquery.com/after/

like image 56
Thiago Belem Avatar answered Nov 09 '22 17:11

Thiago Belem


InsertAfter is what you are looking for:

var row='<tr><td>val</td><td>val2</td></tr>'; $(row).insertAfter("#mainTable tr:first"); 
like image 22
Dexter Avatar answered Nov 09 '22 18:11

Dexter