I’ve an HTML page with a number of tables, one for each user in the database.
The first four columns of each table are radio buttons, with the values: G, A, X and blank respectively.
What I want to do is set the radio button to e.g. ‘A’ for all rows that are currently blank.
Here was where I started:
var st='A';
$('#user123 input').val([st]);
This works, but it also sets to A the rows that were already ‘G’ or ‘X’.
Here is what I’ve tried that didn’t work:
$('#user123 input[value=]').siblings().val([st]);
(nothing gets set)
$('#user123 input[value=]').parent().children().val([st]);
(the blank radio button gets cleared, none get set)
$('#user123 input[value=]').val([st]);
(the blank radio button gets cleared, none get set)
$('#user123 input').not('[value=G]').val([st]);
(planning to add exceptions for A and X later, but it already doesn’t work – all radio buttons get set to ‘A’, not just the blank ones)
I’m really close aren’t I? But I just cannot find the magic incantation I need!
Here is a cut-down version of the table HTML:
<table id="user123">
<tr>
<td><input name="r520" value="G" type="radio"></td>
<td><input name="r520" value="A" type="radio"></td>
<td><input name="r520" value="X" type="radio"></td>
<td><input name="r520" value="" type="radio" checked></td>
<td>2011-06-01</td>
</tr>
<tr>
<td><input name="r767" value="G" type="radio"></td>
<td><input name="r767" value="A" type="radio"></td>
<td><input name="r767" value="X" type="radio"></td>
<td><input name="r767" value="" type="radio" checked></td>
<td>2011-06-03</td>
</tr>
</table>
UPDATE
Thanks for replies; here is the solution I went with (mainly inspired by michelpm’s):
var st='A';
$('#user123 tr').each(function() {
if($("input:radio[value=]:checked",this).length){
$("input:radio[value="+st+"]",this).attr("checked",true);
}
});
If by blank you meant no radio is checked in that row:
http://jsfiddle.net/michelpm/GzPrd/
EDIT:
If you want so bad to do that in one line this one may do the trick…
http://jsfiddle.net/michelpm/GzPrd/2/