Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
118
rated 0 times [  124] [ 6]  / answers: 1 / hits: 16578  / 7 Years ago, sat, april 29, 2017, 12:00:00

I've gathered an Array (I think) of required form elements, and have added 'blur' listener.



    var formInputs = $(':input').filter('[required]');
formInputs.each(function(i) {
$(this).on('blur', function() { // Each time we leave a 'required' field, check to see if we can activate the 'submit' button.
submitEnabler(formInputs);
});
});


So, once someone has left one of these fields, I want to run through this array using .every() and check if the fields are valid - that is if they have a 'success' class that I have defined.



function isValid(input) {
return input.hasClass('is_glowing_success');
}

function submitEnabler(inputs) {

console.log(inputs.every(isValid));
}


I keep getting back:



Uncaught TypeError: inputs.every is not a function
at submitEnabler


Now, I could do something like this...



for (var i = 0; i < inputs.length; i++) {
if ($(inputs[i]).hasClass('is_glowing_success')) {
console.log('yes');
} else {
console.log('no');
}
}


But, why can't I just use: Array.Prototype.every() ?


More From » jquery

 Answers
18

jQuery does not have a .every() method. .every is defined at Array.prototype.



You can use .toArray() to convert jQuery object to an Array, within .every() callback function pass current DOM element to jQuery() to get jQuery object representation of element where .hasClass() can be chained.



function submitEnabler(inputs) {
console.log(inputs.toArray().every(isValid));
}

function isValid(input) {
return $(input).hasClass('is_glowing_success');
}

[#57962] Wednesday, April 26, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kayap

Total Points: 634
Total Questions: 83
Total Answers: 110

Location: Saudi Arabia
Member since Mon, Sep 5, 2022
2 Years ago
;