0

I am trying to get all empty text boxes on my page using jQuery. Somehow I am not able to get all the text boxes that are empty.

I have three text boxes like this

<input type="text" class="test" />
<input type="text" class="test" />
<input type="text" class="test" />

$(".test[value='']") -- this does not returns anything.

if i have this

<input type="text" class="test" value="a" />
<input type="text" class="test" />
<input type="text" class="test" />

$(".test[value='a']") -- this returns one control which is correct.
$(".test[value='']") -- this still returns nothing.

if I type "a" in the second textbox then $(".test[value='a']") -- this still returns only one control.

What wrong am I doing here?

user3731783
  • 718
  • 1
  • 7
  • 31

1 Answers1

1

You should be searching where there is no value attribute, like this:

$('.test:not([value])')

or

$('.test'):not('[value]')

Based on your comments it seems that you want not to get the initial list of elements, but also be able to re-run the selector when someone inputs the value. The issue here is that if you enter the text to input, it does not add value attribute to the DOM. In this case you can use approach similar to what is described in this answer:

$('input:text').filter(function() { return this.value == ""; })

This approach checks the value of the input, not the DOM.

Community
  • 1
  • 1
dotnetom
  • 24,551
  • 9
  • 51
  • 54
  • Thank you. This helped me up to some extent. Its getting the controls fine initially. If I type any text in one of the text boxes then this command still returns all the controls. I have created a jsfiddle http://jsfiddle.net/rsetcma8/2/ . Click on Check after the page is loaded. It displays 3. Enter something in any of the textboxes and hit 'Check' again. It still displays 3. – user3731783 Jan 18 '15 at 08:17