Reputation: 27
I have 2 radio buttons. How can I check it based on one variable value.
<input type="radio" name="team" id="team_7" value="7">
<input type="radio" name="team" id="team_8" value="8">
I have variable var number; which will set 7 or 8 based on some condition.
What I want is if number is 7, radio button team_7 will be checked by default, if it will be 8 radio button team_8 will be check by default.
Upvotes: 0
Views: 2291
Reputation: 48
You can create a dynamic jQuery selector using the number variable you have and select the radio button based on that.
Here is how you can select the default radio button using jQuery. Assuming number is the variable you have.
var number = 'some value (7 or 8)';
$('input[name=team][value=' + number + ']').prop('checked',true);
Upvotes: 1
Reputation: 1229
Just have a if-else statement to set the defaults on load or whenever the function is called.
var num = 8;
if(num === 8){
$('#team_8').prop('checked',true);
}
else if(num === 7){
$('#team_7').prop('checked',true);
}
else{
//do stuff if not 7 or 8
}
Something like this should help.
Upvotes: 0