I have a table where some inputs have to be insert to calculate average of certain numbers and null values have to be excluded. I have almost done it but stuck in the middle. Here are the codes:
function calcAvg(input_id, output_id, co_id) {
//Get all elements with 'class="select"'
var selects = document.getElementsByClassName(input_id);
//Initialize vars
var avg = 0;
var count = 0;
//Calculate average
for (var i = 0; i < selects.length; i++) {
if (selects[i].value != "") {
count++;
avg += Number(selects[i].value);
//Alert for debugging purposes
//alert(selects[i].value+" "+avg);
}
}
avg = avg / count;
//Output average
document.getElementById(output_id).value = avg;
}
<table>
<tr>
<td height="41" colspan="12" align="center">ATTAINMENT OF PO( PO-CO MAPPING)
</td>
</tr>
<tr>
<td width="17%" rowspan="2" align="center">NAME OF THE MODULES</td>
<td height="34" colspan="11" align="center">PROGRAME OUTCOMES</td>
<td height="34" colspan="11" align="center">MODULE CO</td>
</tr>
<tr>
<td width="7%" align="center">PO1</td>
<td width="7%" align="center">PO2</td>
<td width="7%" align="center">CO AVERAGES</td>
</tr>
<tr>
<td height="71" align="center">MATHEMATICS</td>
<td align="center"><input type="number" class="select1" name="value[]" onKeyPress="if(this.value.length==2) return false;" onChange="calcAvg('select1','calculation1','calc_co1');" style="width:60px">
</td>
<td align="center"><input type="number" class="select2" name="value[]" onKeyPress="if(this.value.length==2) return false;" onChange="calcAvg('select2','calculation2','calc_co1');" style="width:60px">
</td>
<td align="center"><input type="text" name="Module_co1" id="calc_co1" readonly style="width:60px"> </td>
</tr>
<tr>
<td height="71" align="center">SCIENCE</td>
<td align="center"><input type="number" class="select1" name="value[]" onKeyPress="if(this.value.length==2) return false;" onChange="calcAvg('select1','calculation1','calc_co2');" style="width:60px">
</td>
<td align="center"><input type="number" class="select2" name="value[]" onKeyPress="if(this.value.length==2) return false;" onChange="calcAvg('select2','calculation2','calc_co2');" style="width:60px">
</td>
<td align="center"><input type="text" name="Module_co2" id="calc_co2" readonly style="width:60px"> </td>
</tr>
<tr bgcolor="#9999CC">
<td height="71" align="center">PO AVERAGES</td>
<td align="center"><input type="text" name="Avg" id="calculation1" readonly style="width:60px"> </td>
<td align="center"><input type="text" name="Avg1" id="calculation2" readonly style="width:60px"> </td>
</tr>
</table>
I have to calculate the average both vertically and horizontally. I have calculated horizontally but stuck in the the vertically calculation. The logic is- the value which are inserted in the vertical position will be the same. I know that the average will be same as the input value but i have to show it in the text box. If there any way to calculate it vertically...here also null value should be excluded.
type="text/jscript". I don't know any browser that would actually evaluate a<script>with that type.calc_co1is not defined. I believe that should be in quotes, likecalcAvg('select1','calculation1',calc_co1);, but didn't know if you meant to do that or not.