views:

33

answers:

2

I have a set of 5 checkboxes with class set to "child". I want that if I select one checkbox, rest of them goes disabled. I have tried following code but it disables the all checkboxes.

if ( !$(this).is ( ":checked" ) ) {
  $(this).removeClass().addClass("hand .parent");
  $(".child").attr ( "disabled" , true );
}

then even i tried adding this

  $(this).removeAttr ( "disabled" );

but it still disables the all controls

help plz! Thanks

A: 

Did you do

$('.child').removeAttr('disabled')

Since you tried it on .child, it's kind of hard to tell the context of this since you're not posting the full code.

Also, are you there there isn't a readonly attribute being set somewhere?

meder
+1  A: 

Are you trying to do something like this?

See it here: http://jsfiddle.net/fEA3Y/

var $cbox = $('.child').change(function() {
    if(this.checked) {
        $cbox.not(this).attr('disabled','disabled');
    } else {
        $cbox.removeAttr('disabled');
    }
});​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

If you really need to toggle classes for some reason, you could do this:

http://jsfiddle.net/fEA3Y/2/

var $cbox = $('.child').change(function() {
    if(this.checked) {
        $(this).toggleClass('child parent');
        $cbox.not(this).attr('disabled','disabled');
    } else {
        $(this).toggleClass('child parent');
        $cbox.removeAttr('disabled');
    }
});​
patrick dw
+1. Did not know that you could assign a variable like that and use it within the event.
Josh Leitzel
Yeah, since the matched set of elements being returned in the jQuery object is needed inside the handler, it's a quick easy way to cache the set instead of re-fetching it every time. It would be the same as doing `var $cbox = $('.child');` on one line, then `$cbox.change( func...` on the next and referencing the same set inside.
patrick dw
Great!!! Millions of thanks!! This is what I was looking for.
Jabran
@Jabran - Does this still work? I thought I had noticed that you had Accepted this answer. If there's an issue, let me know. :o)
patrick dw
I have taken the first box of code and it's working pretty well as I needed. Much thanks for helping out.
Jabran
@Jabran - Glad it worked. Remember to "Accept" this answer by clicking the checkbox to the left of it. :o)
patrick dw