I basically try to check if all input fields are valid by using the following code:
addData (input){
var isValid = false;
for (var i = 0, ii = input.length; i < ii; ++i) {
if ($('#' + input[i].id).hasClass('valid')) {
isValid = true;
}
}
if (isValid) {
//do stuff
}
}
In total the input.length is 3 and I want to check if all 3 input fields are valid. In my example, it just checks the last input element. In related posts, I couldn`t find a way to solve it with a for-loop. Can anybody help?
Thanks for all the helpful answers: Finally, I adjusted Máté Safranka's answer:
var isValid = input.toArray().every(element => $('#' + element.id).hasClass('valid'))
with
.toArray()
to convert jQuery object to anArray
>> see here for more information. Definingtrue
orfalse
is not needed anylonger.