views:

57

answers:

2

hi everybody, I've a form with some radio button like this:

<input type="radio" name="leva" value="a"><input type="radio" name="leva" value="b">

with ajax post method I receive value of the radio. The question is how can I set correct radio value to checked?

thanks in advance ciao h.

+2  A: 

You just need to find the right button using an attribute equals selector for the value, then set checked attribute using .attr(), like this:

$("input:radio[name='leva'][value='"+ returnedValue +"']").attr('checked', true);

The browser will handle un-selecting the other options since it's a radio button, and if no button is found with the value...well, like all jQuery selectors, it just won't have any effect, which is usually what's desired.

Nick Craver
You may want to throw in `[name='leva']` too, to make sure you don't check radio buttons on other groups.
Kobi
@Kobi - fair point, better to make it as precise as possible, updated.
Nick Craver
A: 

That's may be done also for id or I have to use name?

Thanks again.

ciao

h

haltman