0

I want to calculate the total prices of products using the input text values.

The html code and javascript snippet are as follows:

$('input').keyup(function(){ // run anytime the value changes

var firstValue = parseFloat($('#value1').val()) || 0; // get value of field
var secondValue = parseFloat($('#value2').val()) || 0; // convert it to a float



var subtotal1 = firstValue * 10;
var subtotal2 = secondValue * 15;

    
$('#subtotal1').html(subtotal1); // output it
$('#subtotal2').html(subtotal2); // output it
$('#finaltotal').html(subtotal1 + subtotal2); // output it
});
<!-- Product 1 -->
		<div>						
			<label>Product 1</label>
			<div><input type="text" id="value1"><?php echo ' x 10USD = ';?><span id="subtotal1"></span>USD</div> 

<!-- Product 2 -->
		<div>						
			<label>Product 2</label>
			<div><input type="text" id="value2"><?php echo ' x 15USD = ';?><span id="subtotal2"></span>USD</div> 
	
	<!-- Total -->
		<div>						
			<label>Total:</label>
			<div><span id="#finaltotal"></span>USD</div>
	</div>

The final result should be something like the following: [1 ] x 10USD = 10USD [2 ] x 15USD = 30USD Total: 40USD

However, the above code does not work properly. Is there a good way to get it done?

Thanks for your help.

3
  • You have to elaborate on "does not work properly". Otherwise there is no way for us to know what is happening. Commented Jun 23, 2015 at 22:58
  • 1
    remove "#" from the #finaltotal id Commented Jun 23, 2015 at 23:07
  • @survartheec. Thank you. I will be careful next time. Commented Jun 23, 2015 at 23:32

2 Answers 2

1

Remove hash from id attribute, id="finaltotal"

<label>Total:</label>
<div><span id="#finaltotal"></span>USD</div>

               ^-----------remove-----------

It works!

http://jsfiddle.net/4edbv96r/

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

Comments

0

You had a typo. <span id="#finaltotal"></span> should be <span id="finaltotal"></span>

Updated snippet:

$('input').keyup(function() { // run anytime the value changes
  var firstValue = parseFloat($('#value1').val()) || 0; // get value of field
  var secondValue = parseFloat($('#value2').val()) || 0; // convert it to a float
  var subtotal1 = firstValue * 10;
  var subtotal2 = secondValue * 15;
  $('#subtotal1').html(subtotal1); // output it
  $('#subtotal2').html(subtotal2); // output it
  var finalAnswer = (subtotal1 + subtotal2).toString();
  $('#finaltotal').html(finalAnswer.replace(/\B(?=(\d{3})+\b)/g, ",")); // output it
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Product 1 -->
<div>
  <label>Product 1</label>
  <div>
    <input type="text" id="value1">
    <?php echo ' x 10USD = ';?><span id="subtotal1"></span>USD</div>

  <!-- Product 2 -->
  <div>
    <label>Product 2</label>
    <div>
      <input type="text" id="value2">
      <?php echo ' x 15USD = ';?><span id="subtotal2"></span>USD</div>

    <!-- Total -->
    <div>
      <label>Total:</label>
      <div><span id="finaltotal"></span>USD</div>
    </div>

2 Comments

Thank you very much. I have another question: If the total value exceeds 1000 USD, can I insert a thousand separator (comma)?
You can do this using a regex. "10000000".replace(/\B(?=(\d{3})+\b)/g, ","). I've updated my snippet with an example.

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.