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() ?
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() ?
Share Improve this question asked Apr 29, 2017 at 17:26 CodeFinityCodeFinity 1,3502 gold badges22 silver badges27 bronze badges 4-
1
To improve code, you can write
$(':input[required]').blur(submitHandler);
and make changes insubmitHandler
to accessinputs
. – Tushar Commented Apr 29, 2017 at 17:35 - @Tushar I did: $(':input[required]').blur(submitHandler(formInputs)); Works great! Thanks! The previous code did feel a bit 'icky.' – CodeFinity Commented Apr 29, 2017 at 17:54
-
2
It should be
.blur(function() { submitHandler(formInputs); });
. Otherwise, the handler will be called immediately and not onblur
event.s – Tushar Commented Apr 29, 2017 at 17:58 - Absolutely correct. I got that figured out now, and makes sense. – CodeFinity Commented Apr 29, 2017 at 19:14
3 Answers
Reset to default 5Because jQuery objects have no every
method, and formInputs
is a jQuery object.
If you want an array instead, call get()
to get one.
I've gathered an Array (I think) of required form elements...
No, it's just jQuery object. jQuery objects are very array-like, but they aren't arrays. Worse, they have some array-like methods (such as filter
and map
) that call their callbacks with different arguments than the equivalent Array.prototype
methods.
In isValid
, you'd need to handle the fact you're now dealing with a raw DOM element, which means either wrapping it with a jQuery object and using hasClass
:
function isValid(input) {
return $(input).hasClass('is_glowing_success');
}
or using the DOM's classList
:
function isValid(input) {
return input.classList.contains('is_glowing_success');
}
That latter works on all modern browsers, but not all older ones. However, it can be polyfilled on older browsers. More about that on MDN.
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');
}
I will suggest you use array.map()
for example where input
is the array
input.map(function(input){
return $(input).hasClass('is_glowing_success');
});
this is just an example read more here