3

I want to write a selector which will select
1. Input elements of type radio OR checkbox
2. Which are selected ie. wich have is(':checked') as true
3. and have a some class applied to it.
I come up with following code

 $([':radio'],[':checkbox']).each(function() {
    if($(this).is(':checked'))
    {
        className = $(this).attr('class'); 
        if(className!= "")
        {   
            $('#'+className +' :input').attr('disabled', false);
        }
    }   
  });    

Its workign fine but I am looking more efficient solution as I am selecting all radio and checkbox and filtering over it.
How can I combine all conditions ?

Thanks!


EDIT:
The radio or checkbox can have ANY class ie I want to select all radio or checkboxes which are selected and have atleast one class applied it.

0

5 Answers 5

4

This selector will find any radio or checkbox control that is checked and has some class on it.

$(":radio:checked[class], :checkbox:checked[class]")

You can see it working here: http://jsfiddle.net/jfriend00/bYbnH/

If you need to avoid empty class names (where the attribute exists, but it set to an empty string, then you can use this:

$(":radio:checked[class], :checkbox:checked[class]").filter(function() {
    return(this.className && this.className.length != 0);
})
Sign up to request clarification or add additional context in comments.

4 Comments

is the :checkbox/:radio actually needed? I guess :checked[class] would be enough, or is there an element that can be checked, I'm forgetting? And wouldn't it match elements with empty class attributes also?
@Yoshi - it will match an empty class. Don't know if that's a concern of the OP or not as that doesn't occur in normal markup, though it could happen programmatically. It is possible to have a checked attribute on other types of input controls (not sure why it would be there, but it can be) which will match the selector :checked so this limits it to only radio and checkboxes types. I added a new option that filters out empty class names.
@jfriend00: Thanks for updating on empty class, I didnt thought about it
@jfriend00 it could be possible to have an empty class with just spaces. Would be better to use regexp in filter function: return ! this.className.match(/^\s*$/) . See my answer
2

you can try

$(":radio:checked ,:checkbox:checked").hasClass("someclass")

after the question edit

$(":radio:checked[class] ,:checkbox:checked[class]")

3 Comments

Sorry for putting ambiguous question. I want to select all radio or checkboxes which are selected and have atleast one class applied it.
@Ajinkya - the response I provided should accomplish that for you.
@aro: The input element can have ANY class applied to it. It think your answer is specific to class with name someClass.
1
$(':radio:checked,:checkbox:checked').filter(function(){
     return $(this).attr('class').length>0;
});

Comments

0
$(':radio:checked,:checkbox:checked').filter(function (index) {

    if ( typeof $(this).attr('class') != 'undefined' )
        return true;
    else
        return false;   

}).each(function() {

   alert('i am checked and i am checkbox or radio and i have a class!');

});

UPDATE:

You want to use $.filter():

http://jsfiddle.net/grGHa/1/

HTH

Comments

0

I'd go for

jQuery(":radio:checked ,:checkbox:checked")
  .filter(function() {
    // return true if className is not empty or just whitespace
    return ! this.className.match(/^\s*$/) 
  })

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.