tags:

views:

2089

answers:

4

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

A: 

In your handler you should check the value of onlyTwo.

If it is clicked, you enable the fields, otherwise disable them.

Disabled can be a tricky attribute. It may be safest to remove the attribute to enable it.

Tom Hubbard
thats what i want to know. how to determine if checkbox is 'unchecked'
n00bstackie
this.value or this.checked ought to work.
Tom Hubbard
A: 
$('#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.

tj111
wow, thats awesome. where are u checking for 'toggle'? also, i am getting a syntax error on ')};'
n00bstackie
syntax error is coming when you are closing the second function. on the second last line of the code you posted
n00bstackie
I'm getting a 'array is not definedLine 40' error now at runtime :(
n00bstackie
i dont think that will work, atleast in IE, ie only cares if the disabled attribute is there in order to disable the element, it does not care about the value of it
mkoryak
he must have typed that really fast, array line should be var elements = ['textarea3', 'textarea4', 'radio3', 'radio4'];
mkoryak
Ah thanks mkoryak, I was coding in PHP while answering this question, didn't even think about it.
tj111
I had tried that. I hate to be asking simple syntax questions but i suck at jquery :(. Now it says elements.each is not a function....
n00bstackie
+2  A: 

or

$('#onlyTwo').click(function(){
   var stuff = $("#textarea3,  #textarea4, #radio3, #radio4");
   if($(this).is(":checked")}
    stuff.attr("disabled", true);
   else {
    stuff.removeAttr("disabled");
   }
 });
mkoryak
if($(this).is(":checked")} had to be changed to if($(this).is(":checked")) but other than that it works! thanks!
n00bstackie
A: 
$('#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.

coma