views:

40

answers:

3

How do you calculate the number of input boxes with no value in a table row using jquery?

example:

<table id="table1">
<tr class="data" id="row5">
<td><input type="text" value="20%" /></td>
<td><input type="text" value="10%" /></td>
<td><input type="text" value="" /></td>
<td><input type="text" /></td>
</tr>
<table>

I'm looking for answer = 2

+3  A: 

Even that the OP claimed the answer as correct and working, this comes from the api doc:

Some other elements, on the other hand, are empty (i.e. have no children) by definition: input, img, br, and hr, for example.

So actually, it should be impossible to do it with the :empty selector.

$('input:empty').length

or to be more specific:

$('#table1').find('input:empty').length

even more specific:

$('#table1').find('input[type=text]:empty').length
jAndy
i think it's clear that `with no value in a table row` above... 'row' :)
Reigel
are you sure about that? :p I guess I have to correct my answer, it seems like the :empty returns 'empty' by definition for input elements
jAndy
ahh yes.. I have no doubt with the `:empty` at all... it's just that the question title says in a table row... not in a table.... ahh never mind.. :)
Reigel
Am I out of line here? :empty really does work for empty values in input boxes?
jAndy
title : `Count number of empty input boxes in table row`. In A Table Row... that is just my point... the OP's example is just 1 row... but I see it has an id of row5.. by that I can say there are lot's of row (`tr`)... and the OP wanted it by row... as in the title of the question above... not in a table... maybe like `$('#table1 tr').each(function() { $(this).find('input:empty').length });`
Reigel
yes, that is correct. i retested everything and :empty does not work as first thought. thank you for the explanation
Daniel Brink
@Daniel Brink - please look at my demo in my answer...
Reigel
I'm now using .find('input:text[value=""]').length and it seems to work.
Daniel Brink
+1  A: 

You can do like:

alert($('#table1 input:text[value == ""]').length);
Sarfraz
i think it's clear that `with no value in a table row` above... 'row' :)
Reigel
@Reigel: Ultimately input will go inside the table>tr>td.
Sarfraz
but I guess your's at the moment will count inputs in a table...
Reigel
@Reigel: Nope, it counts empty text boxes in the table.
Web Logic
ahhh yeah... It's just I think he wanted it by rows... `..table row..` from above...
Reigel
it's in the title of the question.... ;) `...table row`...
Reigel
A: 
$('tr :text[value=""]').length

or

$('#table1 tr').each(function() { alert($(this).find(':text[value=""]').length) });

quick demo.

Reigel
I will just smile... go down-votes... :)
Reigel