Smudger
Smudger

Reputation: 10809

javascript to disable /enable a checkbox

I have the below syntax which unchecks a checkbox.

$('#h12_1').attr('checked','checked');

h12_1 is the name of my checkbox.

How do I disable the checkbox so it cannot be checked? similarly how do I enable the checkbox again?

Something like this:

 $.post('get_as_12', {data:selectedObj.value},function(result) {
 alert(result[0]) ;

      if(result[0] > 0) {
          $('#h12_1').attr('checked','enabled');
          $('#h12_1').attr('checked','checked');
       } else {
          $('#h15_1').attr('disabled');
          $('#h15_1').removeAttr('checked');
      }
 }); 

Upvotes: 1

Views: 149

Answers (2)

RGR
RGR

Reputation: 1571

To disable checkbox, use disabled attribute.

$("#h12_1").attr("disabled", "disabled");

To enable it back again use removeAttr().

$('#h15_1').removeAttr('disabled');

Upvotes: 1

VisioN
VisioN

Reputation: 145458

For disabling you should use disabled attribute or property (the latter is preferrable):

$("#h12_1").attr("disabled", "disabled");  // removeAttr("disabled") to enable
// or
$("#h12_1").prop("disabled", true);        // false to enable

Upvotes: 5

Related Questions