Neither of the current answers benefit from short circuiting as soon the condition is met. This probably only amounts to a marginal hit on performance for a finite number of fields in JavaScript. But also, neither are great at declaratively stating the intention of the loop you're entering.
Here's a simple 4 line extension method that takes in any filter criteria and returns true as soon as something matches that criteria.
<!-- language: lang-js -->
jQuery.fn.any = function(filter){
for (i=0 ; i<this.length ; i++) {
if (filter.call(this[i])) return true;
}
return false;
};
Then use it like this
<!-- language: lang-js -->
var gotMatch = $(":input").any(function() {
// you can put any condition you want in here
return this.value == 'hi';
});
<!-- begin snippet: js hide: true console: true babel: false -->
<!-- language: lang-js -->
jQuery.fn.any = function(filter){
for (i=0 ; i<this.length ; i++) {
if (filter.call(this[i])) return true;
}
return false;
};
$(function() {
var gotMatch = $(":input").any(function() {
return this.value == 'hi';
});
if (gotMatch) {
console.log("Hello to you too!");
} else {
console.log("Why don't you say Hi!");
}
})
<!-- language: lang-html -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" value="">
<input type="text" value="">
<input type="text" value="">
<!-- end snippet -->
Further Reading: