Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if all textarea are empty using jquery

Tags:

jquery

How can I check whether a group of textareas are all empty using jquery and the textareas class "inside"? I need to check this inside a if.

<textarea id="1" class="inside"></textarea>
<textarea id="2" class="inside"></textarea>
<textarea id="3" class="inside"></textarea>
<textarea id="4" class="inside"></textarea>
like image 391
user111671 Avatar asked Nov 18 '25 16:11

user111671


2 Answers

Something like this would do it

if (! $('.inside').filter(function() {return this.value.length;}).length ) {
   // all empty
}

FIDDLE

Filters out any elements that doesn't have a value, and you could optionally use trim to account for whitespace as well "return $.trim(this.value).length;", and if the selector has no length, none of the elements have a value.

like image 183
adeneo Avatar answered Nov 20 '25 13:11

adeneo


You could use .is(':empty') to check.

EXAMPLE HERE

$('textarea.inside').each(function(){
    if($(this).is(':empty')){
        alert('empty');
    }
});

It's worth noting that the class of each element shouldn't contain a .. Also, an id shouldn't start with a number.

like image 38
Josh Crozier Avatar answered Nov 20 '25 13:11

Josh Crozier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!