YasMan YasMan - 6 months ago 12
HTML Question

How to store Javascript output into a form

Hi I wanted to know how I could store a javascript calculation into a form instead of it being an alert or just written out in another page? Below is my Javascript code, which calculates price.

<script type="text/javascript">
function getQuote() {
var qtyvar = document.getElementById("qty").value;
var qlyvar= document.getElementById("qly").value;
var quote;
if (qlyvar=="basic") {
quote=10*qtyvar;
}
else if (qlyvar=="medium") {
quote=15*qtyvar;
}
else if (qlyvar=="high") {
quote=20*qtyvar;
}
alert('£' + quote);


}


This script is run when this button is pressed

<p><input class="mybutton" name="Quote" value="Calculate quote" onclick="getQuote();"></p>


How can I store the result of the Javascript into the button or inside the form? Hopefully I explained this clearly

Answer

You can have a hidden input field inside the form

<input name="quotation" id="quotation" type="hidden">

And in place of alert in the Js, you can have

document.getElementById("quotation").value = quote;

For your reference - http://plnkr.co/edit/5tV02Tbc6YWTiYKJOtVE?p=preview