tags:

views:

54

answers:

5

Hi

How would I write a selector that would look through say a div of textboxes and find which ones have a value in the textbox(so length would be greater than zero).

Can I do this all in one selector or do I have to get all textboxes then loop through them?

+2  A: 
$("input:text").filter(function() { return $(this).val().length > 0; })

The filter function allows you to provide a custom function which will be called with each currently selected element as this, and only the elements for which it returns true will stay in the set.

Matti Virkkunen
A: 

You can do like:

$('input[type="text"]').each(function(){
  if ($(this)).val().length > 0)
  {
     alert('I have value !!');
  }
});
Sarfraz
+2  A: 
$('textarea[value!=""]')

Of course, if it's an input, you can just modify it:

$('input:text[value!=""]')
Matt
A: 
$('input[value!=""]')
Darin Dimitrov
Won't that show hiddens as well?
Konerak
+1  A: 

Following should work:

nonempty_inputs = $(':input:not(:empty)');

If it's only textareas you are interested in, just change :input to textarea

See also:

azatoth