Reputation: 7792
I'm trying to get the radio buttons that has a certain class that is selected.
Getting the radio buttons of that class comes with
$("input:radio.someClass");
I thought that this would work to get the selected radio button -
$("input:radio.someClass:selected");
But that returns empty - what am I doing wrong?
Upvotes: 13
Views: 37981
Reputation: 56
Simply like this:
let values = [];
$('.yourclass:checked').each(() => {
let radiovalue = $(this).val();
values.push(radiovalue);
});
Upvotes: 0
Reputation: 711
This works for me.
var template_id = $('input[name=template_id].checkbox-tools:checked').val();
$.ajax({
url: campaign_email_url,
type: 'POST',
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
data: {
template_id: template_id,
},
success: function (data) {
alert('Campaign updated!!');
},
error: function (data) {
alert('Campaign error!!');
}
});
Upvotes: 0
Reputation: 8171
This may solve your problem.
$('input[name=radioName].someClass:checked').val();
OR
$("input[type='radiobutton'].someClass:checked");
If not then comment.
Upvotes: 3
Reputation: 12683
According to JQuery documentation.
The :selected
selector works for option elements. It does not work for checkboxes or radio inputs;
Try:
$("input:radio.someClass:checked");
Upvotes: 35
Reputation: 5747
try this,
$('input[class=someClass]:checked', '#yourForm').val();
For best performance jquery documentation recommends Jquery Documentation using type instead of :radio selector
$('input[name=radioName]:checked', '#yourForm').val();
Upvotes: 2
Reputation: 1527
select all checked radio buttons having someclass and then loop through all and get their value
var v= $('input[type=radio].someclass:checked');
$(v).each(function(i){
alert($(this).val())
});
Upvotes: 5
Reputation: 3498
instead of this
$("input:radio.someClass:selected");
try this one
$("input:radio.someClass:checked");
Upvotes: 3