I'm attempting to disable a submit button untill the user has filled out the input fields in the form.
I found THIS thread here which had a really good answer. I'm having just a little problem getting the submit button the re-enable when the fields are filled out.
Can someone please take a look at this function and help me figure out what I'm missing? Any help would be greatly appreciated :D Thank you.
Heres a Fiddle also
$(document).ready(function() {
var $submit = $("input[type=submit]");
if ( $("input:empty").length > 0 ) {
$submit.attr("disabled","disabled");
} else {
$submit.removeAttr("disabled");
}
});
<form method="POST" action="<%=request.ServerVariables("SCRIPT_NAME")%>">
User Name: <input name="Username" type="text" size="14" maxlength="14" /><br />
Password: <input name="last_name" type="password" size="14" maxlength="14"><br />
<input type="submit" value="Login" name="Submit" id="loggy">
</form>
Use the keyup event to check the value before running the condition:
http://jsfiddle.net/tovic/He4Kv/23/
Some of these answers here are a lil out of date as I was looking for a jQuery snippet. I tested them and they do not seem to function properly anymore due to deprecations I think.
So I made my own and here is a newer working way (jQuery >=3.0) which listens to on input event for example.
A plain JavaScript example is here.
In your question I don't see the code where you check the state of inputs constantly, I think the problem is that.
You can use live events to do that. Using your code as example:
Working sample
Instead of blur you can also use keyup like:
Also you can combine multiple events: