with jquery we can simply add new row in top or in bottom using append and prepend method.
html
<a href="#">Add row In the bottom</a> | <a href="#">Add row In the Top</a> <table border="1"> <tbody> <tr> <td>Text 1</td><td>text 2</td> </tr> </tbody> </table>
then add this jquery function to html code
<script type="text/javascript">
$(document).ready(function(){
$("a.addrowb").click(function(){
$("table tbody").append("<tr><td>new row on bottom</td><td></td></tr>");
return false;
})
$("a.addrowt").click(function(){
$("table tbody").prepend("<tr><td>new row on top</td><td></td></tr>");
return false;
})
})
</script>
try it out, and you will see the row will append when you click the link.
full code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="scripts/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("a.addrowb").click(function(){
$("table tbody").append("<tr><td>new row on bottom</td><td></td></tr>");
return false;
})
$("a.addrowt").click(function(){
$("table tbody").prepend("<tr><td>new row on top</td><td></td></tr>");
return false;
})
})
</script>
<title>Untitled Document</title>
</head>
<body>
<a href="#">Add row In the bottom</a> | <a href="#">Add row In the Top</a>
<table border="1">
<tbody>
<tr>
<td>Text 1</td><td>text 2</td>
</tr>
</tbody>
</table>
</body>
</html>
Download the sample here
Advertisement
0 Responses to “Add New Row on Table with jquery”