3

I have a function using jquery that updates a form when an image is click.

I need to add to that function so that a specified radio button is selected.

How can I select a radio button using jquery please?

UPDATE:

I'll explain further...I have overlayed an image and hid the radio button...so what the user clicks on an image it will check the selected radio.

Here's how each radio looks like:

<label style="display: inline; " for="select_theme_a">
<img src="images/icons/theme1.png" />
<input checked="checked" class="select_control" id="select_theme_a" name="select_theme" type="radio" value="a" onclick="return submitForm();" style="display:none" />
</label>

There's 4 radio buttons .. 4 different images to click on.

Hope this helps

Satch3000
  • 47,356
  • 86
  • 216
  • 346

2 Answers2

6

How can I select a radio button using jquery please?

You can use attr method like this:

$('#radio-id').attr('checked', true);
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
  • I cannot use the id in this way as there are 4 radio buttons...only 1 can be checked but how does it know which one? Know what I mean? – Satch3000 Aug 17 '11 at 21:23
  • 1
    @Satch: You can apply id to one or you can use `$('#radio-id.eq(index)')` to target by index. You need to replace index with digit for the radio you want to target. – Sarfraz Aug 17 '11 at 21:39
  • I just want to target, whichever radio get's selected. – Satch3000 Aug 17 '11 at 22:03
0

If radio element is inside same parent element as image than you can use this piece of code inside function that handles image click event:

$(this).parent().find(":radio").attr('checked', true);
longchiwen
  • 822
  • 6
  • 11