Nate Pet
Nate Pet

Reputation: 46222

jquery checking a radio button based on value

I have code that does the following:

$("#ProgramLvl:checked").val() == 3

How do I actually go about actually selecting the check box?

I know we can do:

    $("#ProgramLvl").prop("checked", true);

but how do I check it for 3.

Upvotes: 0

Views: 53

Answers (2)

Shahzad
Shahzad

Reputation: 2063

You can get the radio button using the following:

$('input[type="radio"][value="3"]:checked')

Note: I've used input[type="radio"] because #ProgramLvl will not work. You are checking for an id which will always have only 1 element associated with it.

Assuming your radio buttons have the name of ProgramLvl, then you can use the query as:

$('input[name="ProgramLvl"][value="3"]:checked')

Upvotes: 0

Nick
Nick

Reputation: 147166

Assuming you are talking about a set of radio buttons, you can access the one you want via its name and value attributes:

$('input[name="ProgramLvl"][value="3"]').prop('checked', true);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
1<input type="radio" name="ProgramLvl" value="1">
2<input type="radio" name="ProgramLvl" value="2"> 
3<input type="radio" name="ProgramLvl" value="3"> 
4<input type="radio" name="ProgramLvl" value="4"> 
5<input type="radio" name="ProgramLvl" value="5">

Upvotes: 1

Related Questions