Disable and enable button through textarea?

Let say

How to do this in jquery?
When the textarea is empty the button is disabled.
And when the textarea has text or texts content, it will enable the button.
Meaning the button is clickable?

Please show me sample codes.

thanks in advance

You’ll need to attach a keyup handler to the textarea and set the button’s disabled property depending on whether the value of the textarea is equal to an empty string or not.

Note: use keyup instead of keypress. This gets all the key codes when the user presses something

<textarea id="target"></textarea>
<button>My Button</button>

and

var $button = $("button");
$button.prop("disabled", true);

$("#target").keyup(function(){
  $button.prop("disabled", (this.value === "")? true : false);
});

Demo

1 Like

@James_Hibbard

thank you sir.

I very appreciated your help to me.

1 Like

Just for the record, jQuery doesn’t make much of a difference here… ;-)

// Use more specific selectors here
var button = document.querySelector('button'),
    textarea = document.querySelector('textarea');

button.disabled = true;

textarea.addEventListener('keyup', function() {
  button.disabled = !this.value;
});

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.