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

user113716
  • 318,772
  • 63
  • 451
  • 440
Jabran Rafique
  • 335
  • 1
  • 4
  • 18

2 Answers2

1

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');
    }
});​
user113716
  • 318,772
  • 63
  • 451
  • 440
  • +1. Did not know that you could assign a variable like that and use it within the event. – Josh Leitzel Aug 28 '10 at 00:55
  • 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. – user113716 Aug 28 '10 at 01:03
  • @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) – user113716 Aug 30 '10 at 14:19
  • I have taken the first box of code and it's working pretty well as I needed. Much thanks for helping out. – Jabran Rafique Sep 03 '10 at 22:54
  • @Jabran - Glad it worked. Remember to "Accept" this answer by clicking the checkbox to the left of it. :o) – user113716 Sep 03 '10 at 23:11
0

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 omuraliev
  • 183,342
  • 71
  • 393
  • 434