13

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?

1
  • use $("input:radio.someClass:checked")
    – Buzz
    Dec 3, 2012 at 4:57

8 Answers 8

35

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");
1
  • i want in javascript not jquery
    – namratha
    May 24, 2018 at 6:43
5

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())
});
3

instead of this

$("input:radio.someClass:selected");

try this one

$("input:radio.someClass:checked");
3

This may solve your problem.

$('input[name=radioName].someClass:checked').val();

OR

$("input[type='radiobutton'].someClass:checked");

If not then comment.

2

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();
1

Use this script

$("input:radio.classNmae:checked").val();
1
0

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!!');
      }

   });
0

Simply like this:

let values = [];

$('.yourclass:checked').each(() => {
  let radiovalue = $(this).val();
  values.push(radiovalue);
});  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.