<!DOCTYPE html>
<html>
<head>
<title>calculate average of two students</title>
<script>
function calc()
{
var n1 = parseFloat(document.getElementsByName('n1').value);
var n2 = parseFloat(document.getElementsByName('n2').value);
var n3 = parseFloat(document.getElementsByName('n3').value);
var n4 = parseFloat(document.getElementsByName('n4').value);
var n5 = parseFloat(document.getElementsByName('n5').value);
var n6 = parseFloat(document.getElementsByName('n6').value);
var n7 = parseFloat(document.getElementsByName('n7').value);
var n8 = parseFloat(document.getElementsByName('n8').value);
var oper = document.getElementById('operators').value;
{
document.getElementById('result').value = n1/2 + n2/2;
}
}
</script>
<style>
table{
width: 50px;
border: 1px solid black;
border-collapse: collapse;
}
td{
border: 1px solid black;
padding: 0px;
font-size: 10px;
}
th{
border: 1px solid black;
padding: 0px;
}
form{
width: 4px;
}
div{
display: grid;
}
label {
display: block;
width: 4px;
font-size: 14px;
}
input [type="date"] {
width: 4px;
}
</style>
</head>
<body>
<table>
<tr>
<th rowspan="1" colspan="1">%<br>Sulphur</th>
<th rowspan="1" colspan="1">%<br>Carbon</th>
<th rowspan="1" colspan="1">Average<br>Sulphur</th>
<th rowspan="1" colspan="1">Average<br>Carbon</th>
</tr>
<tr>
<td><input type="text" name="n1" size="3" onkeyup="calc();">1</td>
<td><input type="text" name="n3" size="3" onkeyup="calc();">3</td>
<td>
<input type="text" id="result" size="3.5" rowspan="2" colspan="1" readonly>5</td>
<td><input type="text" id="result" size="3.5" rowspan="2" colspan="1" readonly>6</td>
<td rowspan="2" colspan="1"></td>
</td>
</tr>
<tr>
<td><input type="text" id="n2" size="3" onkeyup="calc();">2</td>
<td><input type="text" id="n4" size="3" onkeyup="calc();">4</td>
</tr>
<tr>
<td><input type="text" name="n5" size="3" onkeyup="calc();">7</td>
<td><input type="text" name="n7" size="3" onkeyup="calc();">9</td>
<td><input type="text" id="result" size="3.5" rowspan="2" colspan="1" readonly>11</td>
<td><input type="text" id="result" size="3.5" rowspan="2" colspan="1" readonly>12</td>
</td>
</tr>
<tr>
<td><input type="text" id="n6" size="3" onkeyup="calc();">8</td>
<td><input type="text" id="n8" size="3" onkeyup="calc();">10</td>
</tr>
</table>
</body>
</html>
please refer to my table on my html table below to understand the question.
I created a table and for clarity`s sake i have numbered all my text boxes.i am trying to calculate the average of text input1 (% sulphur) and text input 2 (% sulphur) to get average result in a readonly text input5(average sulphur).this is for sulphur.
i am also trying to calculate the average of text input3(%carbon) and text input 4(%carbon) to get verage result in a readonly text input 6(average carbon).this is for carboon.
furthermore i want javascript to repeatedly do this to my next text inputs on the table which are input7 and input8 to get average in readonly input11.
the formular should also do the same to add input 9 and 10 to get average in readonly input12
2
Answers
After looking at your code, I think I got what you tried to do.
Is is something like this? I can explain more of what I’m doing if you are interested.
If you were to adopt
dataset
attributes it would be possible to relate input and output elements in such a way that calculations could be done with any number of inputs. The notes within the javascript below should explain what is happeningI appreciate that you are a beginner with Javascript so much of what you see here might well be very daunting and confusing. That said as your knowledge increases you will hopefully find value in some of the techniques shown here – but if you have questions you can ask away!