I have customer site with a set of customized procedures that I maintain. We have a process we use for converting energy for a specific calibration. We take a reading, multiply it by an offset, then enter the result into the test form. The page we have only has limited functionality so I am trying to use HTML form as well as JavaScript for the calculations.
Below is what I have but for some reason the result isn’t showing up to the right of each of the readings that get entered. I only dabble a little with coding so I’m probably missing something obvious.
<!DOCTYPE html>
<html>
<body>
<h2>OAI Offset Caclculation</h2>
<script>
function oaicalc(offset, reading1, reading2, reading3, reading4, reading5) {
//value near 1.000
var offset = parseFloat(document.getElementById('offset').value);
//Energy value in mj 200.0> value <1.000
var r1 = parseFloat(document.getElementById('reading1').value);
var r2 = parseFloat(document.getElementById('reading2').value);
var r3 = parseFloat(document.getElementById('reading3').value);
var r4 = parseFloat(document.getElementById('reading4').value);
var r5 = parseFloat(document.getElementById('reading5').value);
//Corrected Energy Values
var o1 = offset * r1;
var o2 = offset * r2;
var o3 = offset * r3;
var o4 = offset * r4;
var o5 = offset * r5;
//Chage HTML form Output
document.getElementById('output1').innerHTML = o1;
document.getElementById('output2').innerHTML = o2;
document.getElementById('output3').innerHTML = o3;
document.getElementById('output4').innerHTML = o4;
document.getElementById('output5').innerHTML = o5;
}
</script>
<form id="oaioffsetform">
<label for="offset">Offset:</label><br>
<input type="number" id="offset" name="offset" value="0">
<br>
<br>
<label for="reading1">OAI Reading 1:</label><br>
<input type="number" id="reading1" name="reading1" value="0" oninput="oaicalc()">: <output name="output1">0</output>
<br>
<br>
<label for="reading2">OAI Reading 2:</label><br>
<input type="number" id="reading2" name="reading2" value="0" oninput="oaicalc()">: <output name="output2">0</output>
<br>
<br>
<label for="reading3">OAI Reading 3:</label><br>
<input type="number" id="reading3" name="reading3" value="0" oninput="oaicalc()">: <output name="output3">0</output>
<br>
<br>
<label for="reading4">OAI Reading 4:</label><br>
<input type="number" id="reading4" name="reading4" value="0" oninput="oaicalc()">: <output name="output4">0</output>
<br>
<br>
<label for="reading5">OAI Reading 5:</label><br>
<input type="number" id="reading5" name="reading5" value="0" oninput="oaicalc()">: <output name="output5">0</output><br><br>
</form>
</body>
</html>
2
Answers
As was mentioned in the comments, your form fields have
name
attributes but you are attempting to access them as if they hadid
attributes. You can access an element by itsname
attribute by using.querySelector()
.You really should strive to avoid using
id
attributes on elements as their use makes the solution brittle and causes problems with scaling an application down the line. In most cases, you can avoid them completely.Your HTML is not correct for a few reasons:
<head>
element in your document.h1
andh2
would onlythen be used if you were trying to define a sub-section of that
h1
.Do not use heading elements because of the way they style the text,
you can use CSS to alter the styling. Headings are for creating
document structure that is used by those who rely on assistive
technologies, like screen readers to help them navigate the page
content.
br
elements to create visual alignment onthe page.
br
elements are to be used solely to break up a line ofblock level content (i.e., paragraphs, divs, etc.) that must appear
on the next line down. They should not be used to add white vertical
space to a page. Again, use CSS for that.
not use inline event attributes like
oninput
in your HTML. Instead,do your event binding in JavaScript with
.addEventListener()
..innerHTML
should be avoided whenever possible as there are security and performance implications to using it. Since the data you are working with doesn’t contain any HTML that needs to be parsed, you should use.textContent
.See updated code below:
Here I created a general event listener for input on the form. The input element is the target on the event object. You don’t need to parse the value of the input, now that they have the type of number.
If the offset is updated you need to run through all the input elements to calculate a new value.
General comment: use the name attribute for both form and input elements. I makes if easy to find the right element.