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
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);
});
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.