I have an array of input fields containing carton quantity values, and I want to make sure no more cartons are deducted than needed. It works when there is only one input field, for example when I have:
var input = document.getElementsByName('number_of_cartons');
However I can’t get it work for an array of values.
Here is my code and I would appreciate any feedback. Thanks.
var input = document.getElementsByName('number_of_cartons[]');
for (var i = 0; i < input.length; i++) {
var new_cartons = input[i];
var existing_cartons = 25;
var cartons_needed = 10;
$(new_cartons).on('keyup', function () {
if ((new_cartons - existing_cartons) > cartons_needed)
alert("Maximum number of cartons needed is " + cartons_needed + ".");
});
}
I can probably change the array of inputs to individual inputs, (i.e. from number_of_cartons[ ] to number_of_cartons_1, number_of_cartons_2, number_of_cartons_3, etc.), however the whole site is using the array version and it would mess up everything, unless I change it all over the site.
Edward B. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.