How to update the general value every time I add another value in jQuery?

var real_price = $('#price').text(); // getting values from total
var s1_price = $("input[name=s_sub_service]:checked").val(); // getting checked radio button value.
$('#price').text(parseInt(s1_price) + parseInt(string1)); // showing total value in price id.

Here I add different values ​​and give a general meaning. The problem is that when I add a new value to the final value, it also gives me the old value and adds it to the final value.

+4
source share
1 answer

That should work.

$("input").on("change keyup", function(e) {
  var checkboxVal = parseInt($("input[type='radio']:checked").val(), 10);
  var price = parseInt($("#price").val(), 10);
  if (isNaN(price)) {
    price = 0;
  }
  var finalPrice = price + checkboxVal;
  $("#out").html(finalPrice);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="radio" name="group1" value="5" checked>5
<input type="radio" name="group1" value="10">10
<input type="radio" name="group1" value="15">15
<input type="radio" name="group1" value="20">20
<br/>
<input type="text" id="price">
<div id="out">
</div>
Run code
+1
source

Source: https://habr.com/ru/post/1664152/


All Articles