Reputation: 1656
I have 2 radio buttons in html. I want to get the 'value' of whichever radio button is checked.
<input type="radio" name='match' value='all' checked>Match <b>All</b> Tags</input>
<input type="radio" name='match' value='any'>Match <b>Any</b> Tags</input>
Using jquery, I have tried all of the following:
var matchAll = $("input[type='radio']:checked").val();
var matchAll = $("input[type='radio'][name='match']:checked").val();
var matchAll = $("input:radio[name='match']:checked").val();
And they each give roughly the same error:
Error: Syntax error, unrecognized expression: input[type=='radio']:checked
Error: Syntax error, unrecognized expression: input[type=='radio'][name=='match']:checked
Error: Syntax error, unrecognized expression: input:radio[name=='match']:checked
Upvotes: 0
Views: 5644
Reputation: 172
<div id="taskmaster-task_status_id_edit" aria-required="true">
<label class="radio-inline"><input name="TaskMaster[task_status_id]" value="1" type="radio"> Completed</label>
<label class="radio-inline"><input name="TaskMaster[task_status_id]" value="2" type="radio"> Cancelled</label>
<label class="radio-inline"><input name="TaskMaster[task_status_id]" value="3" type="radio"> Rescheduled</label>
<label class="radio-inline"><input name="TaskMaster[task_status_id]" value="4" type="radio"> Queued</label>
</div>
<script>
//some condition,you have to put for every value 1,2,3,etc
$('#taskmaster-task_status_id_edit').find(':radio[name="TaskMaster[task_status_id]"][value="4"]').prop('checked', true);
</script>
I am 4 years late but if it helps someone I had a similar problem. It can be done using the above method. Remember the name has to be in double quotes or it won't work.
Upvotes: 1
Reputation: 6787
Using simple javascript:
<script>
function val(matchAll)
{
document.getElementById("op").innerHTML=matchAll;
}
</script>
<input type="radio" id="I1" onclick="val(I1.value);" value="all" />Match All Tags
<input type="radio" id="I2" onclick="val(I2.value);" value="any" />Match Any Tags
<div id="op"></div>
Upvotes: 0
Reputation: 822
try again with code below:
var match = $('input[name=match]:checked').val();
or
$('input[name=match]:checked').each(function() {
console.log($(this).val());
});
Upvotes: 1
Reputation: 113
You can try this:
var value = "";
$("input[type='radio'][name='match']").each(function(){
if($(this).is(":checked"))
value = $(this).val();
});
You get the value of the checked one only...
Upvotes: 0