jQuery detect if textarea is empty
if (!$("#myTextArea").val()) {
// textarea is empty
}
You can also use $.trim
to make sure the element doesn't contain only white-space:
if (!$.trim($("#myTextArea").val())) {
// textarea is empty or contains only white-space
}
if (!$.trim($("element").val())) {
}
I know you are long past getting a solution. So, this is for others that come along to see how other people are solving the same common problem-- like me.
The examples in the question and answers indicates the use of jQuery and I am using the .change listener/handler/whatever to see if my textarea changes. This should take care of manual text changes, automated text changes, etc. to trigger the
//pseudocode
$(document).ready(function () {
$('#textarea').change(function () {
if ($.trim($('#textarea').val()).length < 1) {
$('#output').html('Someway your box is being reported as empty... sadness.');
} else {
$('#output').html('Your users managed to put something in the box!');
//No guarantee it isn't mindless gibberish, sorry.
}
});
});
Seems to work on all the browsers I use. http://jsfiddle.net/Q3LW6/. Message shows when textarea loses focus.
Newer, more thorough example: https://jsfiddle.net/BradChesney79/tjj6338a/
Uses and reports .change(), .blur(), .keydown(), .keyup(), .mousedown(), .mouseup(), .click(), mouseleave(), and .setInterval().
You just need to get the value of the texbox and see if it has anything in it:
if (!$(`#textareaid`).val().length)
{
//do stuff
}