i’m currently working on an exercise for a class i’m auditing and i was supposed to make a form where the user inputs three numbers and those numbers get added up, averaged, and multiplied. after that, i’m supposed to display the user input numbers and the results of those operations.
however, innerhtml isn’t changing anything upon submitting the form. what should i do to fix that?
thanks so much in advance for the help!
this is what my code looks like:
<html>
<head>
<script type="text/javascript">
function runNumbers(){
var num1 = document.getElementById("number1").value;
var num2 = document.getElementById("number2").value;
var num3 = document.getElementById("number3").value;
var n1 = parseInt(num1);
var n2 = parseInt(num2);
var n3 = parseInt(num3);
var sum = n1+n2+n3;
var ave = sum/3;
var pro = n1*n2*n3;
if(isNaN(n1), isNaN(n2), isNaN(n3)){
document.getElementById('calculations').innerHTML = "<h2>error!</h2><p>you put numbers in there, right?</p>"
} else {
document.getElementById('calculations').innerHTML = "<h2>okay, so according to my calculations...</h2><p>the numbers you entered were "+n1+", "+n2+", and"+n3+", "+"the sum of your numbers is "+sum+", "+"the average of your numbers is "+ave+", "+"and the product of your numbers is "+pro+".</p>";
}
}
</script>
</head>
<body>
<div class="assignment">
<h2>gimme three numbers and i'll do some analysis.</h2>
<form name="gimme">
<label for="number1">my first number is </label>
<input type="text" id="number1" name="number1"> .
<label for="number2">my second number is </label>
<input type="text" id="number2" name="number2"> .
<label for="number3">my third number is </label>
<input type="text" id="number3" name="number3"> .
<input type="submit" onsubmit="runNumbers();" value="run my numbers!">
<input type="reset"value="reset">
</form>
<div id="container">
<div id="calculations"></div>
</div>
</div>
<body>
</html>
2
Answers
Your code has a problem since you are utilizing the
onsubmit
property on the submit button, which is not the correct approach to handle form submission. Instead, you should utilize the form’sonsubmit
event. In addition, there is a minor issue in your HTML structure; the ending/body>
tag should be substituted with/html>
. Here’s the updated code:In this code, I added the
event
option to therunNumbers
method and usedevent.preventDefault()
to prevent the page from refreshing due to the default form submission behavior.Your code looks good for capturing user input from a form and performing calculations with JavaScript. However, there’s a minor issue with your HTML structure – you have a
<body>
tag within the<body>
tag. You should close the first<body>
tag before the second one. Here’s the corrected HTML structure:I made the following changes:
Corrected the
<body>
tag structure.Added
return false;
to theonsubmit
attribute of the form element to prevent it from actually submitting the form, as you want to handle the submission with JavaScript.Fixed the condition in the
if
statement to use||
(logical OR) instead of,
(comma) when checking if any of the input values is not a number.Improved the formatting of the error message and calculation results.
With these changes, your form should work as expected, displaying the values entered into the form and the calculated values when the "Run my numbers!" button is clicked.