1

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

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

7 Comments

Where would I put inside the form? After the Calculate quote?
The input will already be there inside the form. Only the value will be getting updated whenever you click on the button.
Hi @nikhil sorry i tried your method, but I cannot see the hidden field with the calculation appear after I press the button.
@YasMan - I have added a working plunker in answer for your reference. Please check.
Hi thanks for the answer in plunker but when I click calculate, it doesn't show the result of the calculation.
|
3

You can put hidden field in your form, then when click the button set the hidden field value with result.

<input type='hidden' id='result_hdn' value=''>

Then is code:

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

6 Comments

Hi Sorry, I tried using your method but when I press the get quote button, nothing shows up
Your button mustn't submit the form, does it do?
HI @Gouda no that button is used, just to calculate the price and give the quote. Before it was being outputted in an alert box and I want the output to be in a field.
Did you put the HTML hidden field in your form?
Sorry where would I put that? is it when i declare the form?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.