manisha manisha - 7 months ago 15
Javascript Question

Jquery get id of radio button by testing value

I have a read only form that I want to populate the values returned from an ajax query. What I don't understand is the correct syntax to get the id of a specific radio button. I know what value exists in the db, but how to get the id of the radio button, to change the radio button attribute?

For example, in the Radio Button options with the "name"==Criteria1Score", how to I return the id of the input where value ==Good.

Here is an example of the form structure:

<legend>Criteria1</legend>
<label class="radio inline">
Great
<input id="Criteria1Score1" class="Score" name="Criteria1Score" type="radio" value="Great">
</label>
<label class="radio inline">
Good
<input id="Criteria1Score2" class="Score" name="Criteria1Score" type="radio" value="Good">
</label>
<label class="radio inline">
Bad
<input id="Criteria1Score3" class="Score" name="Criteria1Score" type="radio" value="Bad">
</label>

Answer

$('input[type=radio][name=Criteria1Score]:checked').attr('id')

See fiddle example