JorgeeFG
JorgeeFG

Reputation: 5961

Check empty input jquery - loop through array of elements

I've added a new property to input that I require to be filled: data-required (in spanish).

I have 3 or 4 in my form, I loop them this way:

$('#impFormularios').submit( function() {
    $(":input[data-requerido]").each( function() {
      if( $(this).val == '' ) {
        alert('hola');
      }
    });
  });

But my alert is not showing. I think it's because i'm doing something wrong with val since the console log shows JQuery code for each item.val value (in console.log).

Thanks!

Upvotes: 2

Views: 1614

Answers (1)

Adil
Adil

Reputation: 148120

You have to use val() instead of val with jQuery object. You can use value attribute as well but with DOM object.

Change

if( $(this).val == '' ) {

To

if( $(this).val() == '' ) {

Or

if( this.value == '' ) {

Upvotes: 5

Related Questions