Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
100
rated 0 times [  104] [ 4]  / answers: 1 / hits: 15164  / 11 Years ago, mon, may 20, 2013, 12:00:00

I'm trying to check if ALL inputs (.productTextInput) have a value of zero.



If they are all zero then execute an alert, but only if they are ALL zero.



I have been able to check the first and all individually, but not all together.



HTML



<div class=container>
<div class=listQuantity>
<input type=text class=productTextInput name=AddToCart_Amount value=0>
</div>
<div class=listQuantity>
<input type=text class=productTextInput name=AddToCart_Amount value=0>
</div>
<div class=listQuantity>
<input type=text class=productTextInput name=AddToCart_Amount value=0>
</div>
</div>


Script



$('.productTextInput').each(function() {
if ($(this).val() <= 1){
alert(Must choose at least one product);
} else {
// do something
}
});

More From » jquery

 Answers
110

You can use filter for this:



if ($('.productTextInput').filter(function() {
return parseInt(this.value, 10) !== 0;
}).length === 0) {
// They all have zero
}
else {
// At least one has a non-zero value
}

[#78128] Friday, May 17, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
wilson

Total Points: 27
Total Questions: 93
Total Answers: 93

Location: Tajikistan
Member since Sun, Aug 29, 2021
3 Years ago
wilson questions
Tue, Aug 9, 22, 00:00, 2 Years ago
Wed, May 11, 22, 00:00, 2 Years ago
Wed, May 20, 20, 00:00, 4 Years ago
Wed, May 13, 20, 00:00, 4 Years ago
;