erwan
erwan

Reputation: 48

How to get textareas with content?

I've got some textareas in my page.

<textarea id="first">Some content</textarea>
<textarea id="second"></textarea>
<textarea id="third">other content</textarea>

I would like to get all the textareas with any content i.e. except textareas without any content. In my example, it means first and third textarea.

Upvotes: 1

Views: 43

Answers (4)

DanKodi
DanKodi

Reputation: 3640

You can do this,

$(document).ready(function () {
var $elements = $('textarea:not(":empty")');


$elements.each(function () {
    var element = $(this);
    alert(element.val());
});

});

http://jsfiddle.net/G6LCY/

Upvotes: 0

AKD
AKD

Reputation: 3966

var $res = $('textarea').filter(function(i, ele){
    return $(ele).text() != '';
});

OR

var $res = $('textarea').filter(':not(:empty)');

fiddle: http://jsfiddle.net/qYbvr/ http://jsfiddle.net/qYbvr/1/

Upvotes: 0

Rashmin Javiya
Rashmin Javiya

Reputation: 5222

Try this way,

$("textarea:not(:empty)")

Upvotes: 1

James Donnelly
James Donnelly

Reputation: 128781

Simply combine jQuery's not() method with its :empty selector:

$('textarea').not(':empty');

Upvotes: 3

Related Questions