Jquery get id of radio button by testing value
Asked Answered
D

6

16

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>
Diaconal answered 21/5, 2013 at 23:56 Comment(0)
D
47

$('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" />

JSFiddle

Daune answered 22/5, 2013 at 0:5 Comment(2)
Thanks all of these answers work. I was missing the multi attribute selector. Now I got it working.Diaconal
For anyone browsing the correct version for this with modern jQuery versions is: $('input[type=radio][name=Criteria1Score]:checked').prop('id')Greenhaw
K
4

I got it by using checked.

var selected_Id = $('input[name="Criteria1Score"]:checked').attr('id');

if ($('input[name="Criteria1Score"]:checked').val() == 'Good') {
    //do something
}
Kohl answered 5/5, 2016 at 6:11 Comment(0)
H
1
$("input[value='Good']").attr("id");

If you want to select the input by its value

Heilungkiang answered 22/5, 2013 at 0:3 Comment(0)
C
1

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
}
Cellular answered 22/5, 2013 at 0:3 Comment(0)
L
0

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
    }
});
Lynea answered 22/5, 2013 at 0:3 Comment(0)
A
-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

Arabela answered 16/8, 2019 at 11:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.