3

I'm trying to select textareas based on the value in them, I tried doing this:

alert( $('textarea[value="Type"]').length );

But I get zero.

Here's my textarea:

<textarea id="Title" name="title" rows="5" cols="29" class="textentry_verdana12pxItalic">Type</textarea>

Can I do this?

Mark Steudel
  • 1,662
  • 3
  • 18
  • 31

4 Answers4

5

value is not an attribute of that textarea. That textarea's HTML attributes are id, name, rows, cols, and class. Try this instead:

$('textarea').filter(function ()
{
    return $(this).val() === 'Type';
}).length

.filter() API docs

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
1

Use the "contains" pseudo selector:

jQuery('textarea:contains(Type)')
JAAulde
  • 19,250
  • 5
  • 52
  • 63
0

Try alert( $('textarea:contains("Type")').length );

StefanS
  • 876
  • 1
  • 12
  • 21
  • 1
    That will return all textareas which contain the word `Type` anywhere in their value. I don't think that's what the OP wants. – Matt Ball Mar 10 '11 at 19:52
0

try this (contains-selector):

alert($("textarea:contains('Type')").length);
andres descalzo
  • 14,887
  • 13
  • 64
  • 115
  • That will return all textareas which contain the word `Type` anywhere in their value. I don't think that's what the OP wants. – Matt Ball Mar 10 '11 at 19:51
  • does not say that is exactly the value – andres descalzo Mar 10 '11 at 19:59
  • 2
    As I commented on [JAAulde's answer](http://stackoverflow.com/questions/5264994/jquery-select-textarea-based-on-value/5265039#5265039): the selector in the question implies "exactly." – Matt Ball Mar 10 '11 at 20:05