Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Click td, select radio button in jQuery

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!

like image 362
Nick Avatar asked Jan 07 '11 11:01

Nick


People also ask

How to get selected radio button in jQuery?

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.

How to get radio button value on button click in jQuery?

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.

How to check if radio button group is selected?

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.

What is checked in radio button?

The checked property sets or returns the checked state of a radio button. This property reflects the HTML checked attribute.


2 Answers

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);
});
like image 64
Sarfraz Avatar answered Nov 08 '22 08:11

Sarfraz


Try

$(this).find('input:radio').attr('checked','checked');
like image 40
Gerard Banasig Avatar answered Nov 08 '22 07:11

Gerard Banasig