Garrett Garrett - 1 year ago 108
HTML Question

jQuery: How to enable/disable button based on input value?

I'm playing around with jQuery and was wondering how you would enable a button once the user has typed into the input field? and if they delete the contents of the input field then the button will be disabled again.
I've created this little demo to help.

Would I have to use 'keyup' to do this?

$( "#target" ).keyup(function() {
alert( "Handler for .keyup() called." );

Answer Source

I like to use .on() with jQuery, like so:

$("#InputFName").on("keyup", function() {
  $("#BtnNext").prop("disabled", false);
  if( $("#InputFName").val() == '') {
    $("#BtnNext").prop("disabled", true);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download