n00bstackie
n00bstackie

Reputation: 4893

how to perform events when a checkbox is checked/unchecked in jquery

I have the following jquery code which works fine.

$('#onlyTwo').click(function() {
                $("#textarea3").attr("disabled", true);
                $("#textarea4").attr("disabled", true);
                $("#radio3").attr("disabled", true);
                $("#radio4").attr("disabled", true);
                return true;
            });

This is making some fields disabled when 'onlyTwo' checkbox is clicked. How can i make these fields enabed again when 'onlyTwo' checkbox is unchecked...

basically i want to know how to find out whether a checkbox is checked or not

Upvotes: 10

Views: 33733

Answers (3)

mkoryak
mkoryak

Reputation: 57928

or

$('#onlyTwo').click(function(){
   var stuff = $("#textarea3,  #textarea4, #radio3, #radio4");
   stuff.attr("disabled", $(this).is(":checked"));
});

Upvotes: 18

coma
coma

Reputation: 16639

$('#onlyTwo').change(function() {
    $('.disableMe').attr('disabled', $(this).is(':checked'));
});

so you need to add 'disableMe' class to all the inputs, textareas, selects... that you wish to disable.

Upvotes: 5

TJ L
TJ L

Reputation: 24452

$('#onlyTwo').click(function() {
     var elements = ['textarea3', 'textarea4', 'radio3', 'radio4'];
     var checked = $(this).attr('checked');

     jQuery.each(elements, function(element) {
       if (checked) {
         $('#'+element).attr('disabled', true);
       } else {
         $('#'+element).removeAttr('disabled');
       }
     });
})

Thats a simple solution to toggle the disabled attribute on all the elements you want when $('#onlyTwo') is checked.

Fixed array problems, lol this was full of little bugs.

Upvotes: 2

Related Questions