Let's say you have this table a boys list
| Name | Age | Fruit |
|---|---|---|
| Archie | 20 | apple |
| Jimmy | 30 | orange |
and you want to change the table data dynamically using jquery, let's say make it a girls list with the following items
Kristine 18 peach
Vanessa 25 strawberry
and the table will update by cliking the update to girls list button
1. first lets make an html file and paste this 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=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<table width="100%" border="1" background="#eee" cellspacing="1" cellpadding="2">
<tr>
<th bgcolor="#eee"><strong>Name</strong></th>
<th bgcolor="#eee"><strong>Age</strong></th>
<th bgcolor="#eee"><strong>Fruit</strong></th>
</tr>
<tr>
<td>Archie</td>
<td>20</td>
<td>apple</td>
</tr>
<tr>
<td>Jimmy</td>
<td>30</td>
<td>orange</td>
</tr>
</table>
</body>
</html>
<script type="text/javascript" src="jquery.js"></script>
<script></script>
<script>
var msg="<tr><td>Kristine</td><td>18</td><td>peach</td></tr><tr><td>Vanessa</td><td>25</td><td>strawberry</td></tr>";
</script>
<input type="button" value="update to girls list" />
<script>
var msg="<tr><td>Kristine</td><td>18</td><td>peach</td></tr><tr><td>Vanessa</td><td>25</td><td>strawberry</td></tr>";
$(document).ready(function(){
$("input").click(function(){
$( "table tr:first").siblings().remove();
$("table tr:first").after(msg);
});
});
</script>
why tr:first - represents the first row on your table which is the header that containes the Name Age Fruit text
why $("table tr:first").siblings().remove();, we need to remove all the rows below the header first, because we're gonna update it.
why $("table tr:first").after(msg); we insert the value of msg after the header.
7. Youre done, if you click the button it will update.
NOTE: if you do not understand my step by step here's the whole working code just copy and paste it
<!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=iso-8859-1" />
<title>Untitled Document</title>
<script type="text/javascript" src="jquery.js"></script>
</head>
<body>
<script>
var msg="<tr><td>Kristine</td><td>18</td><td>peach</td></tr><tr><td>Vanessa</td><td>25</td><td>strawberry</td></tr>";
$(document).ready(function(){
$("input").click(function(){
$( "table tr:first").siblings().remove();
$("table tr:first").after(msg);
});
});
</script>
<table width="100%" border="1" background="#eee" cellspacing="1" cellpadding="2">
<tr>
<th bgcolor="#eee"><strong>Name</strong></th>
<th bgcolor="#eee"><strong>Age</strong></th>
<th bgcolor="#eee"><strong>Fruit</strong></th>
</tr>
<tr>
<td>Archie</td>
<td>20</td>
<td>apple</td>
</tr>
<tr>
<td>Jimmy</td>
<td>30</td>
<td>orange</td>
</tr>
</table>
<input type="button" value="update to girls list" />
</body>
</html>