I have this html:
<input type="text" name="textField" />
<input type="submit" value="send" />
How can I do something like this:
- When the text field is empty the submit should be disabled (disabled="disabled").
- When something is typed in the text field to remove the disabled attribute.
- If the text field becomes empty again(the text is deleted) the submit button should be disabled again.
I tried something like this:
$(document).ready(function(){
$('input[type="submit"]').attr('disabled','disabled');
$('input[type="text"]').change(function(){
if($(this).val != ''){
$('input[type="submit"]').removeAttr('disabled');
}
});
});
... but it doesn't work. Any ideas? Thanks.
I had to work a bit to make this fit my use case.
I have a form where all fields must have a value before submitting.
Here's what I did:
Thanks to everyone for their answers here.
Please see the below code to enable or disable Submit button
Here's the solution for file input field.
To disable a submit button for file field when a file is not chosen, then enable after the user chooses a file to upload:
Html:
It will work like this:
Make sure there is an 'disabled' attribute in your HTML
The answers above don't address also checking for menu based cut/paste events. Below's the code that I use to do both. Note the action actually happens with a timeout because the cut and past events actually fire before the change happened, so timeout gives a little time for that to happen.
Al types of solution are supplied. So I want to try for a different solution. Simply it will be more easy if you add a
id
attribute in your input fields.Now here is your
jQuery