The
deleteRow(index) approach used here is much better than the ad hoc
removeChild() approach IMO.
Here is my stab at it (untested)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv"Script-Content-Type" content="text/javascript">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Expires" content="0"> <!-- disable caching -->
<title>Example</title>
<script type="text/javascript">
function deleteRows(id, toDeleteHeader) {
var obj = document.getElementById(id);
if(!obj && !obj.rows)
return;
if(typeof toDeleteHeader == 'undefined')
toDeleteHeader = false;
var limit = !!toDeleteHeader ? 0 : 1;
var rows = obj.rows;
if(limit > rows.length)
return;
for(; rows.length > limit; ) {
obj.deleteRow(limit);
}
}
</script>
</head>
<body>
<table id="tbl" name="tbl" border="1">
<thead>
<tr>
<td>No.</td>
<td>Content</td>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Something</td>
</tr>
<tr>
<td>1</td>
<td>Something</td>
</tr>
<tr>
<td>1</td>
<td>Something</td>
</tr>
<tr>
<td>1</td>
<td>Something</td>
</tr>
</tbody>
</table>
<br><br>
<a href="#" onclick="deleteRows('tbl', false);">Delete rows</a>
</body>
</html>
Last edited by ~s.o.s~; Mar 16th, 2008 at 7:17 am.
Super Moderator
Featured Poster
Reputation Points: 3242
Solved Threads: 719
Failure as a human
Offline 8,874 posts
since Jun 2006