jQuery: Make a Button Visible When a TextBox Has Contents

41,808

Sure, the keyup event sounds like a fine idea. You might do something like:

$("textarea").keyup(function() {
  if ($(this).val().replace(/ /g, '') == '')
    $("#id-of-button").show();  
  else
    $("#id-of-button").hide();
});
Share:
41,808
Jim G.
Author by

Jim G.

Updated on August 02, 2022

Comments

  • Jim G.
    Jim G. almost 2 years

    Given: I have a textbox and a hidden button.

    Wanted: When the textbox is neither null nor empty, show the button. When the textbox is null or empty, hide the button.

    Question: How should I do this? Should I use jQuery and bind to the textbox's keyup event?