1

I have looked and can't find a simple answer for my question. I click on one of the TR then click on the other TR and it works fine but after the two have been clicked it no longer recognized the clicking on a row. Any help? Here is the HTML:

<table>
    <th>1</th>
    <th>2</th>
    <tr>
        <td>Yes
            <input type='radio' name='test' value='yes' />
        </td>
    </tr>
    <tr>
        <td>No
            <input type='radio' name='test' value='no' />
        </td>
    </tr>
</table>

and here is the jquery

$(function () {
    $('table tr').click(function () {
        $(this).find('input:radio').attr('checked', 'checked');
    });
});
SomeShinyObject
  • 7,581
  • 6
  • 39
  • 59
Ricky
  • 5,201
  • 4
  • 19
  • 22
  • What are you trying to do? – Aiias Jun 27 '13 at 23:21
  • i want to be able to click a row of a table and have the radio button checked. After one row has been clicked it no longer can be clicked. – Ricky Jun 27 '13 at 23:22
  • If you're using radio buttons, if another row is clicked then the previously clicked row SHOULD be unselected. If this isn't the inteded behavior, you should probably be using checkboxes instead. – relic Jun 27 '13 at 23:24

1 Answers1

6

You need to be setting the checked property via. prop():

$(this).find('input:radio').prop('checked', true);

jsFiddle here.

dsgriffin
  • 66,495
  • 17
  • 137
  • 137
  • Also put a style on the td of cursor:pointer; Just make it look a little nicer :) – user2067005 Jun 27 '13 at 23:23
  • 4
    FYI: to Meta viewers, I didn't mean he *literally* copy-pasted my answer, I simply meant he posted the same solution after a considerable amount of time (plus after an extensive question edit which took place a minute and a half after I'd already answered), and that there is no point in cluttering up questions with duplicate answers. Sorry to Christopher if I offended him, I retract my comments and I'll think twice about posting any negative comments from now on. – dsgriffin Jun 27 '13 at 23:56
  • +1. It is right and you answered very fast. – SomeShinyObject Jun 28 '13 at 00:00
  • Any way we can achieve this without using JavaScript? For e.g. using the label for attribute. – Sid Nov 07 '14 at 11:09
  • @Sid nope, you'll need to use JavaScript – dsgriffin Nov 07 '14 at 15:16