clarkk
clarkk

Reputation: 27685

DOM - append in table

How can I add rows <tr> in to a table a any level(Top/Bottom/In between) using Javascript/jQuery ?

Upvotes: 0

Views: 116

Answers (3)

Jishnu A P
Jishnu A P

Reputation: 14382

$(html).insertBefore($('table tr').eq(index));

Will insert some html before the tr with index specified.

A small demo

Upvotes: 2

StuperUser
StuperUser

Reputation: 10850

http://api.jquery.com/prepend/

Upvotes: 0

Reporter
Reporter

Reputation: 3948

  • to append to the bottom:

you have nothing to do, because you just have to find the parent element and then append it.

  • to append to the top:

well, it is more complicated, but I think is possible. One way is following:

find the parent element from the rows and read all rows into an extra variable. Fortunatly the result will be an array of object. Then create a new row element and use the method unshift() to put this at the begin of the array. Clean table's body and then add each element to body again.

Upvotes: 0

Related Questions