Reputation: 9293
<input type='text' class='inputx'>
<input type='text' class='inputx'>
<input type='text' class='inputx'>
... 20 inputs.
I need to check if any of them is empty.
$('.inputx').each(function(){
if ($(this).val() == '') {console.log('empty');}
});
But empty
is written 20 times !
How can I get only one console writing if any of inputx
is empty?
Upvotes: 0
Views: 27
Reputation: 67207
You can deal with it by using a filter
,
var emptyElements = $('.inputx').filter(function(){
return $(this).val() == '')
});
if(emptyElements.length) console.log("Empty elements found!");
Or you could optimize it by using a simple for loop,
var elems = $('.inputx')
for(var x = 0; x < elems.length; i++){
if(elems.get(x).value.length == 0) break;
}
if(--x != elems.length) console.log("Empty elements found!");
Upvotes: 1
Reputation: 121998
You can try value attr selector
if ($("input:text.inputX[value!='']").length >0) {
// empty elements found
}
Upvotes: 0