2

I got old code of my apps that using javascript to calculate something, the logic is if 1 textbox filled, another textbox automatic filled, and when the second textbox filled, that another textbox will return text1 - text2, maybe You can look at this fiddle:

HTML:

<label>Price Start</label>
<input type="number" class="form-control" placeholder="Total Harga Diberikan" required="required" name="price" onkeyup="calculate()" id="price" />
<br />
<label>Discount</label>
<input type="number" class="form-control" placeholder="Nominal Potongan" name="jmlvoucher" onkeyup="calculate()" id="jmlvoucher" />
<br />
<label>Total</label>
<input type="number" class="form-control" placeholder="Total yang harus dibayarkan" required="required" name="total" id="total" />

Javascript:

function calculate(){
    var num1 = document.getElementById("price").value;
    var num2 = document.getElementById("jmlvoucher").value;

    var sum=parseFloat(num1)-parseFloat(num2);
    document.getElementById('total').value=sum.toString();
}

http://jsfiddle.net/n4anv5qn/2/

Already try to check error, but there's no error. Try to run it, it doesn't works. Any idea why?

3
  • 1
    You did not link to the fiddle Commented May 8, 2015 at 4:59
  • sorry... there's error from stackoverflow Commented May 8, 2015 at 5:00
  • Possible duplicate of Why does this simple JSFiddle not work? Commented Mar 25, 2018 at 19:11

2 Answers 2

3

There indeed is an error being generated as keyup is fired.

Uncaught ReferenceError: calculate is not defined

On JSFiddle you've chosen to put your JavaScript into the onLoad function which is wrong. You should instead choose No Wrap

See http://jsfiddle.net/n4anv5qn/5/

As requested, this code is an example of how you can still calculate your Total if your Discount is not filled. Simply check to see if the value you grabbed from num2 is blank and set it to a default value.

function calculate(){
    var num1 = document.getElementById("price").value;
    var num2 = document.getElementById("jmlvoucher").value;

    if (num2 == '')
        num2 = '0';

    var sum=parseFloat(num1)-parseFloat(num2);
    document.getElementById('total').value=sum.toString();
}
Sign up to request clarification or add additional context in comments.

4 Comments

This is the correct answer. Another option is to define a global function if he uses onLoad event: var target = document || window; target.calculate = function() { /* ... */ };
sorry, there's misunderstanding, if I type only in textbox 1, the third textbox must be filled too... Then if I type in textbox 2, the third textbox will substract text1 with text2... also, how to write nowrap at my php code?
No Wrap in the context of JSFiddle simply means do not put your provided Javascript inside another piece of Javascript code. You can see Psioniax's example of where to put your Javascript. I've also added your request to the answer.
well, after putting the code to bottom my code works well.... thank you... I confused where to put answered marks.. on you or on Psioniax?
0

You should use onchange instead of onkeyup. Also put your scripts right before the closing body tag.

Here's how to do it:

<label>Price Start</label>
		<input type="number" class="form-control"name="price" onchange="calculate()" id="price" />
    <br />
    <label>Discount</label>
	    <input type="number" class="form-control" onchange="calculate()" id="jmlvoucher" />
    <br />
    <label>Total</label>
		<input type="number" class="form-control" required="required" name="total" id="total" />

    <script>
    function calculate(){
		var num1 = document.getElementById("price").value;
		var num2 = document.getElementById("jmlvoucher").value;

		var sum = parseFloat(num1) - parseFloat(num2);
		document.getElementById('total').value = sum;
    }
    </script>

5 Comments

Yeah, but I changed it before moving the script at the bottom. And IMO onchange is better anyway.
@I'mnidhin. onkeyup is a keyboard event, it will fail if the user enters input using the arrow keys beside the input(using mouse).
@Psioniax, in your example, event is being triggerer after the input element goes i blur state wheras if you go with keyup event, calculation will take place after the keyboard event is fired..
What if I use the mouse to click on the arrows? Then there will be no event fired (related to the keyboard).
doesn't work... @Ryan solution is what I want but there's a little misunderstood...

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.