Sometimes even Google can't solve a problem. Then I turn to the sites where the brains wander around, searching for challenges to solve. StackOverflow.com is one of those sites. I've been on that site for a while, but I'm gradually finding how to effectively frame my questions in order to ensure the best responses. For example, I needed some help with Javascript and using it to talley a particular HTML table column for matching values, then write the result in the bottom-most cell of that column. After sifting through quite a few responses, I settled on this one…
<!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" 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>
No comments:
Post a Comment