I have 4 input fields and I need the total of all fields not to exceed 100. I want to set the max of the field being changed.
I have been trying to adjust values on keyup() and then difference the total of the other fields from the current and set the max. Seems to work for a bit then stop.
var $fieldA = $('#acf-field_55dc6669f5235');
var $fieldB = $('#acf-field_55dc6699f5236');
var $fieldC = $('#acf-field_55dc66d3f5237');
var $fieldD = $('#acf-field_55dc6713f5238');
var total;
function getTotal() {
total = parseInt($fieldA.val()) + parseInt($fieldB.val()) + parseInt($fieldC.val()) + parseInt($fieldD.val());
}
$fieldA.change(function () {
$this = $(this);
var input = parseInt($this.val());
var otherFields = parseInt($fieldB.val()) + parseInt($fieldC.val()) + parseInt($fieldD.val());
var max = 100 - otherFields;
$this.attr("max", max);
});
$fieldB.change(function () {
$this = $(this);
var input = parseInt($this.val());
var otherFields = parseInt($fieldA.val()) + parseInt($fieldC.val()) + parseInt($fieldD.val());
var max = 100 - otherFields;
$this.attr("max", max);
});
$fieldC.keyup(function () {
$this = $(this);
var input = parseInt($this.val());
var otherFields = parseInt($fieldA.val()) + parseInt($fieldB.val()) + parseInt($fieldD.val());
var max = 100 - otherFields;
$this.attr("max", max);
});
$fieldD.change(function () {
$this = $(this);
var input = parseInt($this.val());
var otherFields = parseInt($fieldA.val()) + parseInt($fieldB.val()) + parseInt($fieldC.val());
var max = 100 - otherFields;
$this.attr("max", max);
});
getTotal;
fieldChaskeyupwhen others havechange. And reproduce the issue, so that we can fix it .