jQuery disable/enable submit button

Created by Eric Palakovich CarrLinked to 73.5m issues across 115 teams


Here's how to use jQuery to disable and enable a submit button based on the value of an input field.

First, we need to select the submit button and set its disabled property to true. Next, we need to add an event listener to the input field. We can use the keyup event, which will fire whenever a key is pressed and released. Finally, we need to check the value of the input field. If the value is not empty, we want to enable the submit button. We can do all of this with the following code:

$(document).ready(function() { $(':input[type="submit"]').prop('disabled', true); $('input[type="text"]').keyup(function() { if($(this).val() != '') { $(':input[type="submit"]').prop('disabled', false); } }); });

That's it!