I try to create a table with some input with type text. I create a javascript to calculate the grade but dun know something goes wrong. My html code is not working. Can any expert please check with me? I dun want jquery solution. It is not cover in my learning. Thanks in advance. BTW, please help me with something not too complicated. I am just new learner in html and javascript. Thanks again
<html>
<head>
<title>Grade Calculator</title>
<script type="text/javascript">
function calculate(){
var gradeP1 = parseFloat(document.getElementByName("tableG").gradeP1.value);
var gradeP2 = parseFloat(document.getElementByName("tableG").gradeP2.value);
var gradeP3 = parseFloat(document.getElementByName("tableG").gradeP3.value);
var gradeP4 = parseFloat(document.getElementByName("tableG").gradeP4.value);
var credit1 = parseFloat(document.getElementByName("tableG").credit1.value);
var credit2 = parseFloat(document.getElementByName("tableG").credit2.value);
var credit3 = parseFloat(document.getElementByName("tableG").credit3.value);
var credit4 = parseFloat(document.getElementByName("tableG").credit4.value);
if(gradeP1==null || gradeP2==null || gradeP3==null || gradeP4==null ||
credit1==null || credit2==null || credit3==null || credit4==null)
{
alert ("Please Fill All Required Field");
return false;
var total1=gradeP1*credit1;
var total2=gradeP2*credit2;
var total3=gradeP3*credit3;
var total4=gradeP4*credit4;
document.getElementByName("tableG").total1.value=total1;
document.getElementByName("tableG").total2.value=total2;
document.getElementByName("tableG").total3.value=total3;
document.getElementByName("tableG").total4.value=total4;
var totalC=credit1+credit2+credit3+credit4;
document.getElementByName("tableG").totalC.value=totalC;
var totalT=total1+total2+total3+total4;
document.getElementByName("tableG").totalT.value=totalT;
var averageC=totalC/4;
document.getElementByName("tableG").averageC.value=averageC;
var averageT=totalT/totalC;
document.getElementByName("tableG").averageT.value=averageT;
}
</script>
<noscript>
Your browser does not support JavaScript!
</noscript>
</head>
<body>
<table border="1" align="center" name="tableG">
<caption>GPA Calculator for 4 subjects(Please fill up all grade for 4 subjects)</caption>
<tr bgcolor ="yellow">
<th>Course Code</th>
<th>Course name</th>
<th>Grade</th>
<th>Grade Point</th>
<th>Credit Hours</th>
<th>[Credit Hours] X [Grade Points]</th>
</tr>
<tr>
<td><input type="text" name ="code1" value="must fill"/></td>
<td><input type="text" name="name1" value="must fill"/></td>
<td><input type="text" name="grade1" value="must fill"/></td>
<td><input type="text" name="gradeP1" value="must fill"/></td>
<td><input type="text" name="credit1" value="must fill"/></td>
<td><input type="text" size="100%" name="total1"/></td>
</tr>
<tr>
<td><input type="text" name ="code2" value="must fill"/></td>
<td><input type="text" name="name2" value="must fill"/></td>
<td><input type="text" name="grade2" value="must fill"/></td>
<td><input type="text" name="gradeP2" value="must fill"/></td>
<td><input type="text" name="credit2" value="must fill"/></td>
<td><input type="text" size="100%" name="total2"/></td>
</tr>
<tr>
<td><input type="text" name ="code3" value="must fill"/></td>
<td><input type="text" name="name3" value="must fill"/></td>
<td><input type="text" name="grade3" value="must fill"/></td>
<td><input type="text" name="gradeP3" value="must fill"/></td>
<td><input type="text" name="credit3" value="must fill"/></td>
<td><input type="text" size="100%" name="total3"/></td>
</tr>
<tr>
<td><input type="text" name ="code4" value="must fill"/></td>
<td><input type="text" name="name4" value="must fill"/></td>
<td><input type="text" name="grade4" value="must fill"/></td>
<td><input type="text" name="gradeP4" value="must fill"/></td>
<td><input type="text" name="credit4" value="must fill"/></td>
<td><input type="text" size="100%" name="total4"/></td>
</tr>
<tr bgcolor="pink" >
<td> </td>
<td colspan="3">Total</td>
<td><input type="text" name="totalC"/></td>
<td><input type="text" name="totalT"size="100%" /></td>
</tr>
<tr bgcolor="blue">
<td> </td>
<td colspan="3">GPA</td>
<td><input type="text" name="averageC"/></td>
<td><input type="text" name="averageT" size="100%"/></td>
</tr>
</table>
<br />
<input type="button" name="calculate" value="calculate" onclick="calculate()" />
<center>
<img src="image/grade.jpg" alt="Grade rating information" />
</center>
</body>
</html>