Reputation: 783
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
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