0

I have a form with several text fields and I would like the add button to be enabled when the required fields are filled in. The button is disabled by default in the HTML. In Firebug it appears the blur function fires but the that the if statement isn't reached.

$(".cd-form :input").blur(function(){

    var ok=true;
    $("#signup-firstnmame,#signup-surname","#signup-Address1","#signup-City","#signup-Postcode","#signup-Email","#signup-Mobile").each(function(){
       /* if ($(this).val()==="")*/


        if ($('#signup-firstnmame').val() !== "" && $('#signup-surname').val() !== "" && $('#signup-Address1').val() !== "" && $('#signup-City').val() !== "" && $('#signup-Postcode').val() !== "" && $('#signup-Email').val() !== "" && $('#signup-Mobile').val() !== "") 
       $("#AddData").prop("disabled",false);

            else
       $("#AddData").prop("disabled",true);
    });

});
4
  • Does the selector match your HTML? I see several typos in it and inconsistencies for case. Please post HTML with the inputs. Commented Aug 25, 2016 at 13:31
  • I'm pretty sure the if statement is being hit. But a console.log into the if and the else and also console.log the value of each of those inputs. Also, there's no need to use each if you're not going to use $(this). You can remove the if entirely I believe, or check the value with $(this) instead of that bloated if statement. Commented Aug 25, 2016 at 13:32
  • Shouldn't all of your selectors be within a single string? Commented Aug 25, 2016 at 13:36
  • I would add a class to every field you want to capture, Add a boolean defaulted to true everytime you start your loop. loop them with each, store a false if this.val() is empty. Check if the boolen is still true at the end. Enable if true. That should look better. Commented Aug 25, 2016 at 13:37

2 Answers 2

1

The commas are supposed to be a part of the selector, not separate parameters.

$('#signup-firstname, #signup-surname, #signup-Address1, ...

Also, if you're checking all of the fields, as in your if statement, you don't need to do that once for each field, it'll suffice to do it once.

If you would consider adding a class to the relevant fields, your function would be much more readable, i.e:

$('#AddData').prop('disabled', $('.required-field[val=""]').length > 0);
Sign up to request clarification or add additional context in comments.

Comments

0

To Start, I think it would be smart to add a unique class to the inputs that you care about. this way you can do something along the lines of:

$('.yourClass').on('input', function() {
  var x = true;
  $('.yourClass').each(function() {
    this = $(this); //cast it to jQuery obj (ez ref)
    if (this.val() == "")
            x = false;
  } );
} );

Basically, every time someone enters something into the fields, jQuery will iterate through your inputs checking the values and adding 1 to x. If x is equal to the number of elements you are checking then it will enable the button.

This is not the most elegant solution but how I got around the same issue you were having when I was rushed into finishing a project.

Modified my answer with what @JaredT mentioned about the boolean, far more elegant. I am sure this could be improved further though, hope this gets the ball rolling.

Comments

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.