X9DESIGN
X9DESIGN

Reputation: 783

jquery - Calculating field current value with a new one and sum both

I got field <input id="A" type="hidden" value="0" />. By clicking on some element i get another value which i want to sum up with current value of field above { 0 + [another value] }. So if "another value" is 5, the new value of field above should be 5. Each new value taken from next click action should be always a summary of current value of field above and the new value taken from click action. How to do that?

Upvotes: 1

Views: 31

Answers (1)

Atul Sharma
Atul Sharma

Reputation: 10740

$(document).on('click','#B',function() {
    $("#A").val(parseInt($("#A").val())+parseInt($("#B").val()));
});

$(document).on('click','#C',function() {
    $("#A").val(parseInt($("#A").val())+parseInt($("#C").val()));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="A" type="text" value="0" />
<input id="B" type="text" value="10" />
<input id="C" type="text" value="20" />

You can add event handlers to all elements you want to click, and then read the value of main (A) and the element clicked, add them and push back the value to A.

Upvotes: 1

Related Questions