I am working with an input a textarea and a input button . I need that the button is disabled when the input and textarea are empty.
This is Html
<input type="text" class="area">
<textarea name="testo" class="area" cols="30" rows="10"></textarea>
<input type="button" class="btn" value="send" disabled>
Javascript
$('.area').on('keyup' , function() {
if( $('input').val().length > 0 && $('textarea').val().length > 0 ){
$('.btn').prop('disabled', false);
}
else {
$('.btn').prop('disabled', true);
}
});
It works fine, but , if a press the spacebar and so it types blank chars , the lengths are not < 0 anymore and so the button not anymore disabled. How can I prevent this situation?
Use $.trim() to clean up the values off of leading and trailing spaces
if( $.trim($('input').val()).length > 0 && $.trim($('textarea').val()).length > 0 ) {
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With