I have a reservation form that lists classes, with radio buttons, for reserving a class. If the class is full, a waitlist checkbox appears on the row with the class’s radio button. In jQuery, if a full class radio button is clicked, I want jQuery to check the waitlist checkbox. The td class=”wailist” will be in every row, but the checkbox will only be in the HTML if the class is full.
My jQuery so far-
var $class_table = JQuery('table.courses');
$class_table.find('input.radio').click(function (event)
{
//if this row has a td.waitlist with a checkbox in it then check that box
});
Here is the HTML snippet:
<tr class="course_full">
<td class="course_select", colspan=2>
<label>
<input type="radio" name="course[1][1]"
id="course_id_1_9"
value="9"
>
Geometry 2
</label>
</td>
<td class="credit_recovery">
<label>
<input type="checkbox" name="credit_recovery[1][9]"
id="credit_recovery_id_1_9"
>
</label>
</td>
<td class="waitlist">
<label>
<input type="checkbox" name="waitlist[1][9]"
id="waitlist_id_1_9"
>
</label>
</td>
I’ve failed at every ‘this’ ‘closest’ ‘find’ etc. Maybe length needs to be involved?
Is the
clickevent working? If it’s not, I’d suggest the:radioselector:If it is, then the next step is to locate your checkbox relative to the found radio. You can simply use
parentas @Imdad suggested (or several calls to parent) or, alternatively,closest. Inside your callback:It will locate the parent row (no matter how deeply nested your radio button is) and then search for a checkbox inside the column with class
waitlist. The last call will only have any effect if the result offindis non-empty. Working example at jsFiddleIf you prefer to be explicit when navigating the structure, you can locate the correct column with 2 calls to
parentand one tosiblings, according to your HTML snippet:Working example here, should produce the same results as the first one.
(Note: cleaned up the answer, correct contents preserved)