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:
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?
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);
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:
Note: Use id selector or with class use $($('.total_cost')[0])
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 ...
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", "...
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....