Garrett Garrett - 1 month ago 6
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

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

$("#InputFName").on("keyup", function() {
  $("#BtnNext").prop("disabled", false);
  if( $("#InputFName").val() == '') {
    $("#BtnNext").prop("disabled", true);
 }
});