Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery disable/enable submit button

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?

like image 785
kmunky Avatar asked Oct 20 '09 14:10

kmunky


People also ask

How disable submit button until form is filled jQuery?

click(function () { if ($('#submit-button').is(':disabled')) { $('#submit-button'). removeAttr('disabled'); } else { $('#submit-button'). attr('disabled', 'disabled'); } }); In the above code, enabled is the id of the checkbox used and submit-button is the class name of the submit button used .

Is button disabled jQuery?

Checking if the button is disabled or enabled with jQuery removeAttr('disabled'); }; const checkButton = (e) => { const isDisabled = $('#my-button'). prop('disabled'); if( isDisabled ){ alert("Is disabled!"); } else{ alert("Is enabled"); } }; $(document).

How do you disable submit button until form is filled?

Just click f12 in your browser, find the submit button in the html, and then remove the disabled ! It will submit the form even if the inputs are empty.


1 Answers

The problem is that the change event fires only when focus is moved away from the input (e.g. someone clicks off the input or tabs out of it). Try using keyup instead:

$(document).ready(function() {      $(':input[type="submit"]').prop('disabled', true);      $('input[type="text"]').keyup(function() {         if($(this).val() != '') {            $(':input[type="submit"]').prop('disabled', false);         }      });  }); 
like image 154
Eric Palakovich Carr Avatar answered Sep 20 '22 10:09

Eric Palakovich Carr