0

I have added this JQuery - found on SO to validate my form so that fields must be filled before the submit button can be clicked, but the button never un-disables.

Form

<form action="homepage.php" method="POST" id="form"> <div class="medium-50 columns"> <label>Phone Number</label> <input type="text" placeholder="Enter Your Phone Number" name="phone" /> </div> <input type="submit" id="register" value="Submit" name="submit" disabled="disabled"> </form> 

JQuery

(function() { $('form > input').keyup(function() { var empty = false; $('form > input').each(function() { if ($(this).val() == '') { empty = true; } }); if (empty) { $('#register').attr('disabled', 'disabled'); } else { $('#register').removeAttr('disabled'); } }); })() 

What is wrong?

1
  • isn't it $('#register').attr('disabled',true);? Commented Dec 15, 2015 at 14:54

1 Answer 1

2

It doesn't work because 'input' isn't a child of 'form'.

Take a look here and here.

Try this:

(function() { $('form input').keyup(function() { var empty = false; $('form input').each(function() { if ($(this).val() == '') { empty = true; } }); console.log(empty); if (empty) { $('#register').attr('disabled', 'disabled'); } else { $('#register').removeAttr('disabled'); } }); })()
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form action="homepage.php" method="POST" id="form"> <div class="medium-50 columns"> <label>Phone Number</label> <input type="text" placeholder="Enter Your Phone Number" name="phone" /> </div> <input type="submit" id="register" value="Submit" name="submit" disabled="disabled">

Sign up to request clarification or add additional context in comments.

1 Comment

This works, as it should, however I should have described me requirement slightly better. On my form I have a group of checkboxes/radio buttons that are allowed to be left blank and then I have the form with input text boxes which have to be filled in. when I use this answer on mine it does not un disable still - i believe because of the checkboxes

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.