1

I'm trying to programatically select a radio button using jQuery 1.4.2. It works in IE 8 and Chrome 12 but doesn't seem to work in Firefox 5.

The HTML for the radio button is:

<input type='radio' name='selected-row'>

The code I'm using is below, where radioButton is a jQuery object.

    onCellSelect: function (rowid, iCol, cellcontent, e) {
        var cell = $(e.srcElement);
        var radioButton = cell.closest("tr").find("input[type='radio']");

        radioButton.click();
    }

I've also tried using the below based on stuff I found googling and that works in both IE and Chrome, but not Firefox.

 radioButton.attr("checked", "true");
 radioButton.button("refresh");

How does one get this to work in Firefox?

John Mills
  • 10,020
  • 12
  • 74
  • 121
  • You might try the new [.prop()](http://api.jquery.com/prop/) setter to see if you have more luck with `radioButton.prop("checked", true). Haven't worked with FF5 and don't know what version of jQ you're using so I have no idea what to expect. – lsuarez Jun 24 '11 at 01:52
  • 1
    I'm using jQuery 1.4.2 but am not able to upgrade it. – John Mills Jun 24 '11 at 02:18
  • I changed the line `var cell = $(e.srcElement);` to `var cell = $(e.target);` and it now works in all three browsers. – John Mills Jun 24 '11 at 02:36

2 Answers2

1
<input type="radio" value="The Answer" name="choices" id="myRadio">

-

$('#myRadio').attr('checked', true);

Works fine for me in Firefox 5. I think your problem must lie elsewhere. Maybe a different firefox-specific error is preventing it from reaching that line.

takteek
  • 7,020
  • 2
  • 39
  • 70
1

Like takeek's above the following works in Firefox 5

<input id="myRadioButton" type="radio" name="selected-row">

and using works

$('#myRadioButton').attr('checked', true);

Check your radioButton variable is what you expect it to be. How are you selecting your radio button?

Also prop() was only introduced to jQuery in 1.6 so attr() is fine here.

jacob.toye
  • 1,212
  • 11
  • 14