Tuesday, January 26, 2010

JQuery Append a table row at first and last positions of a table

This is something you need to know when you have some requirement to add rows to table on client side using some java script technology. I have chosen JQuery and the requirement is, I need to add the rows at first and last of the existing table. Below is the way to achieve that.

Add as first row of a table:
$(".gridview").prepend("<tr></tr>");
Add as last row of a table:
$(".gridview > tbody:last").append("<tr></tr>");

I think, you feel it's simple. Yes it is easy and helps in solving some issues. And remember the class gridview I have used in this example, is the table class name. And the string "<tr></tr>" is the actual table row content.

Hope this helps and let me know, are there any better ways to implement the same.

No comments:

Post a Comment