Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested Jquery selector

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.

like image 525
Ajinkya Avatar asked Mar 21 '26 23:03

Ajinkya


1 Answers

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);
})
like image 160
jfriend00 Avatar answered Mar 24 '26 12:03

jfriend00



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!