qadenza
qadenza

Reputation: 9293

check empty for a lot inputs and get a single alert

<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

Answers (2)

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

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

Suresh Atta
Suresh Atta

Reputation: 121998

You can try value attr selector

    if ($("input:text.inputX[value!='']").length >0) {
     // empty elements found
    }

Upvotes: 0

Related Questions