Got a small problem with some very basic jQuery, I'd like to be able to click on a table cell, and then have it automatically select the radio button inside.
HTML:
<table>
<tr>
<td>
1<br>
<input type="radio" name="choice" value="1">
</td>
<td>
2<br>
<input type="radio" name="choice" value="2">
</td>
<td>
3<br>
<input type="radio" name="choice" value="3">
</td>
</tr>
</table>
jQuery:
$("td").click(function () {
$(this).closest('input:radio').attr('checked',true);
});
Any help would really be appreciated, thank you!
To check which radio button is selected in a form, we first get the desired input group with the type of input as an option and then the value of this selection can then be accessed by the val() method.
Answer: Use the jQuery :checked selector You can simply use the jQuery :checked selector in combination with the val() method to find the value of the selected radio button inside a group.
Using Input Radio checked property: The Input Radio checked property is used to return the checked status of an Input Radio Button. Use document. getElementById('id'). checked method to check whether the element with selected id is check or not.
The checked property sets or returns the checked state of a radio button. This property reflects the HTML checked attribute.
Use this selector:
$('input:radio', this).attr('checked', true);
Or using find
method:
$(this).find('input:radio').attr('checked', true);
Here is how your code should look like:
$("td").click(function () {
$(this).find('input:radio').attr('checked', true);
});
Try
$(this).find('input:radio').attr('checked','checked');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With