2

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!

0

1 Answer 1

5

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 == '' ) {
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, I still miss to add the () because I think its a property of the element... I will post a sticky note on my monitor lol
You can use value property with DOM object, check my updated answer.
Consider changing == to ===.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.