YasMan YasMan - 2 years ago 66
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") {
else if (qlyvar=="medium") {
else if (qlyvar=="high") {
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 Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download