0
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script>
            var form = document.forms.myform,
                    qty = form.qty,
                    cost = form.cost,
                    output = form.textbox;

            window.calculate = function () {
                var q = parseInt(qty.value, 10) || 0,
                        c = parseFloat(cost.value) || 0;
                output.value = (q * c).toFixed(2);
            };
        </script>

    </head>
    <body>
        <form action="caltest.php" method="post" name="myform" onkeyup="calculate()">
            <label>Num of PAX :</label>
            <input type="text" name="qty" />
            <input type="hidden" name="cost" value="700" />
            <br/>
            <lable>Total Price: </lable>
            <input type="text" name="textbox" />

        </form>
    </body>
</html>

I am doing a simple calculation.Not getting any output.

6
  • wotking fine : jsfiddle.net/aiubian/kfLa17a5 Commented Jan 6, 2016 at 5:46
  • i have tried place in jsfiddle, it work fine... but when i insert this into my netbeans, it dose not give me any output Commented Jan 6, 2016 at 5:48
  • do u get any error? @user3230289 Commented Jan 6, 2016 at 5:49
  • wrap your code with load method Commented Jan 6, 2016 at 5:50
  • write js code in $(document).ready() Commented Jan 6, 2016 at 5:52

2 Answers 2

1

When var form = document.forms.myform is executed, there is no <form name="myform" ...yet.
The simplest (though maybe not the most sophisticated) solution is to move the <script> block from <head> to after the form code.

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

Comments

0

Try this one is working

<html>
    <head>
        <meta charset="UTF-8">
        <title></title>

    </head>
    <body>
        <form action="caltest.php" method="post" name="myform" onkeyup="calculate()" >
            <label>Num of PAX :</label>
            <input type="text" name="qty" />
            <input type="hidden" name="cost" value="700" />
            <br/>
            <lable>Total Price: </lable>
            <input type="text" name="textbox" />
      </form>    
      <script>
            var form = document.forms.myform,
                    qty = form.qty,
                    cost = form.cost,
                    output = form.textbox;

            window.calculate = function () {
                var q = parseInt(qty.value, 10) || 0,
                        c = parseFloat(cost.value) || 0;
                output.value = (q * c).toFixed(2);
            };
        </script>
    </body>
</html>

Comments

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.