I want this form function in HTML to display the result after the Submit button is clicked. At this time, it executes the result when I enter numbers in those three input boxes (width, height, length), but I want to add a submit button, and when someone clicks on it, the result should be shown in a box.
<form oninput="x.value=parseFloat(((a.value)*parseFloat(b.value)*parseFloat(c.value))*1.3).toFixed(2)">
<label for="a">Width (ft)</label> 
<input type="number" id="a" value=""><br><br>
<label for="b">Length (ft)</label>
<input type="number" id="b" value=""><br><br>
<label for="c">Height (ft)</label>
<input type="number" id="c" value=""><br><br>
<button>Submit</button>
<output name="x" for="a b c"></output>
</form>
4
Answers
oninput
toonsubmit
<button>
to<button type="submit">
return false
to cancel submitx.value
tox.innerText
(and putid="x"
instead ofname="x"
)First remove the oninput attribute from the form and add a submit button for calculating when the user clicks. On that submit button you can use the onclick method for calling javascript function. Inside that function, you can do your required calculations. Lastly, to show the result use the output element.
Follow these steps:
You are likely looking for
onsubmit
, which will be triggered by the<button>...</button>
in your form. Be careful though, submitting a form triggers a page refresh. You can cancel this by adding a<form onsubmit="...; return false" >...</form>
at the end of your handler.However, putting your handler in the html isn’t a best practice. I would recommend to add a script tag and put your javascript in there:
Remove the oninput property from the form. To prevent the form from automatically submitting, add a button element with type="button". Create a JavaScript function to carry out the computation and display the result.
Corrected Code: