加熱面 (mm) の横に加熱の回答を表示したいのですが、うまくいきません。Chrome コンソールからのみ次のエラー メッセージが表示されます。
Uncaught TypeError: Cannot read property 'value' of null
アラートボックスを追加したので、他のすべてが機能することはわかっていますが、機能するにはinnerHTMLが必要です。
html は次のとおりです。
<html>
<head>
<script type="text/javascript" src="pipeheaterfunction.js">
</script>
</head>
<body>
<table>
<tr><td>Inner Diameter (mm):</td>
<td><input id="dia" onkeypress="pipeheater();"></td>
<tr><td>Pipe Thickness (mm):</td>
<td><input id="thi" onkeypress="pipeheater();"></td>
<tr><th>Calculate heater:</th>
<td><button onclick="pipeheater();">Calculate</button></td></tr>
<tr><td>Heating Surface(mm):</td>
<td><span class="output" id="surface"></span></td></tr>
</table>
</body>
</html>
JavaScriptコードは次のとおりです。
function pipeheater(){ //dia is the inner diameter of a pipe, thi is the pipe thickness
var dia = document.getElementById("dia").value;
var thi = document.getElementById("thi").value;
var hsur; //hsur is the heating surface required for a certain pipe in mm
hsur = 5*Math.sqrt(((dia-thi)*thi)/2);
var surface = hsur;
if(surface>0){
surface.innerHTML=surface.toFixed(1);
alert(surface.toFixed(1));
}
}
window.onload=pipeheater();