Using Javascript within an HTML page, is it possible to count the instances of a given text value in the cells for a given column? In other words, if one of the columns contains cell values of either "1" or "2", can I generate a "total" count of how many "2" values exist in that column?
Using Javascript within an HTML page, is it possible to count the instances of a given text value in the cells for a given column? In other words, if one of the columns contains cell values of either "1" or "2", can I generate a "total" count of how many "2" values exist in that column?
Share Improve this question edited Sep 18, 2018 at 15:58 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Feb 3, 2011 at 18:11 SkatterbrainzSkatterbrainz 1,0876 gold badges24 silver badges31 bronze badges 3- Are you looking for the total in a column or row? You refer to both in your question. – JoeyRobichaud Commented Feb 3, 2011 at 18:44
- Both answers to this question (as of writing) feature a jQuery-based implementation of JavaScript. Are you using a library at all, or do you require plain JavaScript? – David Thomas Commented Feb 3, 2011 at 18:56
- @David: You're right. Somehow I thought I saw a jQuery tag. Updated my answer. – user113716 Commented Feb 3, 2011 at 20:04
3 Answers
Reset to default 3EDIT: Regarding your ment:
Example: http://jsfiddle/cGS99/
var rows = document.getElementById('myTable').rows,
len = rows.length,
i,
cellNum = 0,
count = 0,
cell;
for (i = 0; i < len; i++) {
cell = rows[i].cells[cellNum];
if (cell.innerHTML === 'X') {
count++;
} else if(cell.innerHTML === '...') {
cell.innerHTML = count;
}
}
or if by the column with "...", you meant the last column, change this:
} else if(cell.innerHTML === '...') {
to this:
} else if(i === (len - 1)) {
Example: http://jsfiddle/cGS99/1/
doh!
var rows = document.getElementById('myTable').rows, // get the rows in the table
len = rows.length, // get the quantity of rows
cell = 1, // index of the column (zero based index)
count = 0; // keep score
while( len-- ) {
if( rows[len].cells[cell].innerHTML.indexOf('2') > -1 )
count++;
}
This will work as long as the content doesn't have possible sub-strings of "2", like "12".
Sounds like you are looking for the contains selector. fiddle.
<table>
<tr>
<td>1</td>
<td>2</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>2</td>
</tr>
</table
Use length
$('td:contains(1)').length
$('td:contains(2)').length
Output:
--> 4
--> 2
@patrick_dw pointed me in the direction I needed (thanks!). Here's the result that works for me...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3/1999/xhtml" lang="en" xml:lang="en">
<head>
<title></title>
<script language="Javascript">
function getx() {
var rows = document.getElementById('myTable').rows, // get the rows in the table
len = rows.length, // get the quantity of rows
cell = 0, // index of the column (zero based index)
count = 0; // keep score
while( len-- ) {
if( rows[len].cells[cell].innerHTML.indexOf('1') > -1 )
count++;
}
document.getElementById('colx').innerHTML = count;
}
</script>
</head>
<body onload="getx();">
<table id="myTable" border="1">
<tr>
<td>1</td>
<td>A</td>
</tr>
<tr>
<td>2</td>
<td>B</td>
</tr>
<tr>
<td>1</td>
<td>A</td>
</tr>
<tr>
<td>1</td>
<td>A</td>
</tr>
<tr>
<td>1</td>
<td>B</td>
</tr>
<tr>
<td>2</td>
<td>A</td>
</tr>
<tr>
<td id="colx"> </td>
<td></td>
</tr>
</table>
</body>
</html>
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745079452a4610035.html
评论列表(0条)