Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
176
rated 0 times [  180] [ 4]  / answers: 1 / hits: 25880  / 9 Years ago, fri, february 13, 2015, 12:00:00

Here I have many input text fields with same class like



<input type=text class=MyClass></input>
<input type=text class=MyClass></input>
<input type=text class=MyClass></input>


My requirement is to check wheather all input fields of this class is empty or not.
I've tried this



if(!('.MyClass').val()){
alert(empty);
}


But it doesn't make any result. Can anyone help?


More From » jquery

 Answers
17

You can check





$('button').click(function() {
var $nonempty = $('.MyClass').filter(function() {
return this.value != ''
});

if ($nonempty.length == 0) {
alert('empty')
}
})

<script src=https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js></script>
<input type=text class=MyClass />
<input type=text class=MyClass />
<input type=text class=MyClass />
<button>test</button>





Or using a flag





$('button').click(function() {
var flag = false;
$('.MyClass').filter(function() {
if (this.value != '') {
flag = true;
//no need to iterate further
return false;
}
});

if (!flag) {
alert('empty')
}
})

<script src=https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js></script>
<input type=text class=MyClass />
<input type=text class=MyClass />
<input type=text class=MyClass />
<button>test</button>




[#67843] Wednesday, February 11, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
marisela

Total Points: 103
Total Questions: 105
Total Answers: 102

Location: Solomon Islands
Member since Fri, Oct 8, 2021
3 Years ago
;