I'm trying to iterate over a collection of items with the class required
. I think I must be using the $.each
function incorrectly.
function required(address) {
//object to hold elements not passing validation tests
var pass = true;
$('.required').each(function(index, elem){
console.log(elem);
//check if it has the class indicating it is an email
if (elem.hasClass('re')) {
var validEmail = validateEmail(address.email);
if (!validEmail){
$(this).addClass('nv');
}
}
});
}
I see that the element in the console is
<input type="text" id="name_input" class="pr required">
And then the error
Uncaught TypeError: Object #<HTMLInputElement> has no method 'hasClass'
How can that object not have the hasClass method?
Your first hint should be that the object is an #<HTMLInputElement>
and not a jQuery object. Use $(elem).hasClass('re');
elem
in your code is a DOM Element object, if you want to use jQuery methods you should create a jQuery object first:
if ($(elem).hasClass('re')) {
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With