praks5432
praks5432

Reputation: 7792

Get selected radio buttons of certain class

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

Answers (8)

Tomáš Strejček
Tomáš Strejček

Reputation: 56

Simply like this:

let values = [];

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

Upvotes: 0

Pri Nce
Pri Nce

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

M Tulaib Saleem
M Tulaib Saleem

Reputation: 11

Use this script

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

Upvotes: 1

Ishan Jain
Ishan Jain

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

Akhil Sekharan
Akhil Sekharan

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

Swarne27
Swarne27

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

Ankush Jain
Ankush Jain

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

Murali N
Murali N

Reputation: 3498

instead of this

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

try this one

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

Upvotes: 3

Related Questions