0

I have made an array of element ID's for radio buttons like so:

var buttons = [$('#first'), $('#second'), $('#third'), $('#fourth')];

What I am now trying to do is, check to see if each one is checked and if so, add a class to something, else add a class to something else. I know how do this separately for each individual id:

if($('#first').is(':checked')) { 
    $('.cb-enable').addClass('selected');
}
else {
    $('.cb-disable').addClass('selected');
}

I tried using if($('buttons').is(':checked')) { but it doesn't work.

Can someone please helps me on this one?

5
  • That's an array of jquery objects, not an array of element id's. also, what does $('buttons') have to do with your buttons array? None of your code ever uses the buttons array. Commented Aug 20, 2013 at 19:08
  • buttons.each(function(index, value){value.is(':checked')}); Commented Aug 20, 2013 at 19:12
  • Just out of curiosity, what are .cb-* elements? Commented Aug 20, 2013 at 19:19
  • Are there no satisfactory answers to your question here? You have not accepted or voted them. If anything is unclear, I will be glad to further assist you. Commented Sep 7, 2013 at 16:17
  • my bad, I completely forgot about this question. Commented Sep 7, 2013 at 20:30

5 Answers 5

1

Here is a working demo: http://jsbin.com/UpUk/1/edit

  <button class="check">Tell me what is checked</button>
  <input type="radio" class="checkable">
  <input type="radio" class="checkable">
  <input type="radio" class="checkable">
  <input type="radio" class="checkable">
  <input type="radio" class="checkable">

var $radios = $('.checkable');

$('.check').click(function() {
  $radios.each(function(index, item) {
    if ($(item).is(':checked')) {
      alert('item: '+index+' is checked!');
    }
  });
});

It is generally better to use classes, and you don't need to deal with each item individually to accomplish what you are trying to do.

Here's a better version that deals with switching classes. It may be more than what you're looking for, but I don't know quite what you're using this for.

http://jsbin.com/UpUk/3/edit

That should be plenty to help you understand how to approach your project.

Sign up to request clarification or add additional context in comments.

Comments

1

Try:

var buttons = $('#first:checked,#second:checked,#third:checked,#fourth:checked');
if (buttons.length>0)
{
...
} else {
...
}

Comments

1

You should use

$(elem).prop("checked")

to get true or false value if the radio button is checked or is not checked.

Comments

1

Maybe you can try something like this:

        $('input[type=radio]').each(function (index) {
            if ($(this).is(':checked')) 
               $('.cb-disable').addClass('selected');
            else 
               $('.cb-enable').addClass('selected');
        }

You may not need the index but it will be incremental in order it is found in the dom like what you attempted depending on the details of what your trying to do.

Comments

1
var buttons = [$('#first'), $('#second'), $('#third'), $('#fourth')];

$.each(buttons, function(i,el){
  alert( el[0].checked );
});

DEMO

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.