JavaScript How can get last value of form charge in jQuery

I am trying to make a form where a customer can choose parson and automatically show the cost depend on parson number. So for that, I used jQuery form change function and calculate the cost inside of the function. My all logic is working ok but the issue is when increasing number then showing multiple costs.

Visual look:

enter image description here

Always I want to show the last one/ updated one

Blow my code:

var adultsSingleChage = 200;
var childrenSingleCharge = 100;
var infantsSingleCharge = 50;


$('#absbt_form input').change(function(){
    var adults = $("#absbt_adults").val();
    var adultsCharge = adults * adultsSingleChage;

    var children = $("#absbt_children").val();
    var childrenCharge = children * childrenSingleCharge;

    var infants = $("#absbt_infants").val();
    var infantsCharge = infants * infantsSingleCharge;

    var totalCharge = adultsCharge + childrenCharge + infantsCharge;

    console.log(totalCharge);


    $('.total_cost').append(totalCharge);

});

I know I did the maximum of logic but for last logic, I'm confused.

How can I just show the last one?

Answer:1

append adds new content to existing one - that's the reason of having previous values.

Instead of using append use text:

$('.total_cost').text(totalCharge);
Answer:2

The problem is with the code you are appending which means you are adding the text into the element instead of replacing it. You can use the following two methods:

  1. text(): $('.total_cost').text("iota")
  2. html(): $('.total_cost').html("iota")

Note: Use id selector or with class use $($('.total_cost')[0])

Answer:3

I am trying to get some suggestions when I write something in HTML form. Hence,I call a JS function when onkeypress and onkeyup events are triggered on that form. In JS function I am using HTTP ...

I am trying to get some suggestions when I write something in HTML form. Hence,I call a JS function when onkeypress and onkeyup events are triggered on that form. In JS function I am using HTTP ...

  1. running javascript from command line
  2. running javascript from python
  3. running javascript from java
  4. running javascript from terminal
  5. running javascript from html
  6. running javascript from visual studio code
  7. running javascript from c#
  8. running javascript from php
  9. running javascript from command line windows
  10. running javascript from address bar
  11. running javascript from console
  12. running javascript from url
  13. running javascript from command line linux
  14. running javascript from browser
  15. running javascript from bash
  16. running javascript from powershell
  17. running javascript from chrome
  18. running javascript from command prompt
  19. running javascript from code behind c#
  20. running javascript from notepad

I believe I'm wording this question correctly.... I have a dataset like this... [ { "Fri Aug 03 2018 00:00:00 GMT-0500": { "bill": "Test 2", "account": "defaultAccount", "...

I believe I'm wording this question correctly.... I have a dataset like this... [ { "Fri Aug 03 2018 00:00:00 GMT-0500": { "bill": "Test 2", "account": "defaultAccount", "...

  1. sort nested array javascript
  2. sort nested array php
  3. sort nested array mongodb
  4. sort nested array
  5. sort nested array python
  6. sort nested array of objects javascript
  7. sort nested array js
  8. sort nested array java
  9. sort nested array mongoose
  10. mongodb sort nested array
  11. sort nested array typescript
  12. sort array multidimensional php
  13. sort array multidimensional javascript
  14. sort array multidimensional java
  15. sort array multidimensional
  16. sort array by nested value
  17. sort multidimensional array php by value
  18. sort multidimensional array php by key
  19. sort multidimensional array python
  20. sort multidimensional array c#

My website allow users to signup by their Google or Facebook account. But some users use clone account to signup for spamming, so I want to detect and allow only Facebook or Google account which was ...

My website allow users to signup by their Google or Facebook account. But some users use clone account to signup for spamming, so I want to detect and allow only Facebook or Google account which was ...

I'm trying to figure out why a call to this.setState isn't updating state. I have the following lines method: changeState(state, value){ this.setState({state:value}, ()=>console.log(this....

I'm trying to figure out why a call to this.setState isn't updating state. I have the following lines method: changeState(state, value){ this.setState({state:value}, ()=>console.log(this....

  1. react this setstate is not a function
  2. react this setstate callback
  3. react this setstate not working
  4. react this setstate prevstate
  5. react this.setstate
  6. react this.setstate example
  7. react this.setstate undefined
  8. react this.setstate not updating state
  9. react this.setstate then
  10. react this.setstate async
  11. react this.setstate array
  12. react this.setstate not updating
  13. react this.setstate does not work
  14. react this.setstate function
  15. react this.setstate promise
  16. react this.setstate object
  17. react this.setstate in constructor
  18. react this.setstate in render
  19. react this.setstate multiple
  20. react this.setstate is not defined