Here it is the thing you've mentioned. All the changes I've made in the previous source has been highlighted so that you can understand the difference without any trouble.
Important JS lines are also commented.
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title> </title>
<style type="text/css">
</style>
<script type="text/javascript">
var rowCount = function(countWord){
/*I assume that the first row of the table will hold the column names and I've designed the script based on that if it is not like that it will differ somewhat*/
var rows = document.getElementById('tbl').getElementsByTagName('tr');
var avoidCol = [1]; //This is the array that will hold the column number which you want to avoid from counting use comma to seperate the values.
var cols = rows[1].getElementsByTagName('td').length;
if(avoidCol.length >= cols){return;}
var result = new Array(cols - avoidCol.length );
for(var m = 0; m < cols; m++){
result[m] = 0;
}
//Counting the total number mentioned word
for(var i = 1; i < rows.length; i++){
var tds = rows[i].getElementsByTagName('td');
for(var j = 0; j < tds.length; j++){
if(inArray(avoidCol, j + 1)) continue;
if(tds[j].childNodes.length == 1 && tds[j].childNodes[0].nodeValue == countWord)
result[j]++;
}
}
//Appending the result value in the last row's columns
var td = rows[rows.length - 1].getElementsByTagName('td');
for(var k = 0; k < td.length; k++){
if(inArray(avoidCol, k + 1)) continue;
td[k].appendChild(document.createTextNode(result[k]));
}
}
var inArray = function(arr,fval){
for(var i = 0; i < arr.length; i++){
if(arr[i] == fval)
return true;
}
return false;
}
window.onload = function(){
rowCount('Book'); //Pass the word which you want to count
}
</script>
</head>
<body>
<table cellpadding="0" cellspacing="0" border="1" width="400" id="tbl">
<tr>
<th width="100">New Field</th>
<th width="100">First Field</th>
<th width="100">Second Field</th>
<th width="100">Third Field</th>
</tr>
<tr>
<td width="100">Book</td>
<td width="100">Mate</td>
<td width="100">Book</td>
<td width="100">Book</td>
</tr>
<tr>
<td width="100">Image</td>
<td width="100">Book</td>
<td width="100">Book</td>
<td width="100">Book</td>
</tr>
<tr>
<td width="100">Image</td>
<td width="100">Book</td>
<td width="100">Book</td>
<td width="100">Book</td>
</tr>
<tr>
<td width="100">Image</td>
<td width="100">Video</td>
<td width="100">Image</td>
<td width="100">Book</td>
</tr>
<tr>
<td width="100">Book</td>
<td width="100">Book</td>
<td width="100">Image</td>
<td width="100">Book</td>
</tr>
<tr>
<td width="100"></td>
<td width="100"></td>
<td width="100"></td>
<td width="100"></td>
</tr>
</table>
</body>
</html>
Bookmarks