Reputation: 607
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>
Upvotes: 17
Views: 73764
Reputation: 4887
$('input[type=radio][name=Criteria1Score]:checked').attr('id')
Working Demo:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<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>
<input type="button" onclick="alert($('input[type=radio][name=Criteria1Score]:checked').attr('id'))" value="click me to get id of checked input" />
Upvotes: 48
Reputation: 1
Jquery get id of radio button by testing value,
<legend>You are ready for</legend>
<label class="radio inline">
Success
<input id="success" class="win" name="successvalue" type="radio" value="Great"/>
</label>
<label class="radio inline">
Big Success
<input id="bigsuccess" class="win" name="successvalue" type="radio" value="Good"/>
</label>
<br/>
<input type="button" onclick="alert($('input[name=successvalue]:checked').attr('id'))" value="click me too" />
See fiddle
Worked as code to get id value on checked radio button
Upvotes: -1
Reputation: 514
I got it by using checked.
var selected_Id = $('input[name="Criteria1Score"]:checked').attr('id');
if ($('input[name="Criteria1Score"]:checked').val() == 'Good') {
//do something
}
Upvotes: 4
Reputation: 11714
You can try
var selected_Id = $('input[name="Criteria1Score"]:selected').attr('id');
EDIT: Sorry didn't see the second part. If you want to check the value == Good you can do
if ($('input[name="Criteria1Score"]:selected').val() == 'Good') {
//do something
}
Upvotes: 1
Reputation: 2222
$("input[value='Good']").attr("id");
If you want to select the input by its value
Upvotes: 1
Reputation: 104775
You can loop through those inputs to do what you need:
$("input[name='Criteria1Score']").each(function() {
if (this.value == "Good") {
//do something with this input
}
});
Upvotes: 0