I have no idea about loop in jquery. I want the following code to be run as for loop from product1 to product20 and check the same if quantity1 to quantity20 have no value then set it to 1.
$('#product1').change(function() {
if( $('#quantity1').val().length == 0 ){
$('#quantity1').val("1");
}
});
$('#product2').change(function() {
if( $('#quantity2').val().length == 0 ){
$('#quantity2').val("1");
}
});
Thank you in advance.
I think that the
eachmethod is the best solution here, as you need an anonymous function anyway to create a closuse that holds the value for each iteration (as the event handler runs later, each one needs its own version of the loop counter):