Enabling/Disabling ASP.NET checkbox using JavaScript

Now this is very strange.

Well, it seemed that way at first anyway :)

Assume you have declared in your ASP.NET page some web controls as below:

<asp:checkbox id="chkFirstCheck" runat="server" textalign="Left" text="Check me first!" />
<br />
<asp:checkbox id="chkSecondCheck" runat="server" textalign="Left" text="I'm second!!!" enabled="false" />

And also there is a bit of JavaScript on the ASP.NET page that looks like this:

<script language="javascript">
function getObj(name)
{
/* DHTML Micro API
* Source: http://www.quirksmode.org/js/dhtmloptions.html
*/
if (document.getElementById) // test if browser supports document.getElementById
{
this.obj = document.getElementById(name);
this.style = document.getElementById(name).style;
}
else if (document.all) // test if browser supports document.all
{
this.obj = document.all[name];
this.style = document.all[name].style;
}
else if (document.layers) // test if browser supports document.layers
{
this.obj = document.layers[name];
this.style = document.layers[name].style;
}
}

function checkedChanged()
{
var firstCheck = new getObj('chkFirstCheck');
var secondCheck = new getObj('chkSecondCheck');

if (firstCheck.obj.checked == true)
{
secondCheck.obj.disabled = false;
}
else
{
secondCheck.obj.disabled = true;
secondCheck.obj.checked = false; // if the secondCheck is disabled, then it should not be checked
}
}
</script>

In the code behind, for the ASP.NET page, in your Page_Load, you’d probably be attaching Javascript to the checkboxes, as below:

chkFirstCheck.Attributes.Add("onclick", "checkedChanged();")

Ok, now what is the expected behaviour?

I’d say the expected behaviour would be, you check the first check box, and the second one should be enabled, which then allows you to be able to check the second one.

Right?

Actually, very wrong.

If you actually looked at the source code produced by ASP.NET, you’d most likely come across something similar to the below:

<label for="chkFirstCheck">Check me first!</label><input id="chkFirstCheck" type="checkbox" onclick="checkedChanged();" />
<br />
<span disabled="disabled">
<label for="chkSecondCheck">I'm second!!!</label>
<input id="chkSecondCheck" type="checkbox" disabled="disabled"/>
</span>

The major problem that you will likely come across is with the <span disabled=”disabled”>.

It will disable the entire element. Our JavaScript will only enable the checkbox, but there is also another <span> tag in there that is disabled.

The SPAN tag encapsulates the label and input tags inside it, as it is the parent tag.

So what we actually require is a way of enabling and disabling the parent SPAN tag.

After doing a bit of googling, I came across this post by Andrew Connell.

It looked like what I needed, but I made some adjustments to fit my requirements.

So what was needed was two extra lines of JavaScript.

Inside of the if statement, put the below:
secondCheck.obj.parentNode.disabled = false;

And, inside the else statement, add the below:
secondCheck.obj.parentNode.disabled = true;

Note that I use parentNode rather than parentElement.disabled in the above code, as parentElement.disabled will not work in Firefox.

To get the above to work in IE4, you’ll need parentElement rather than parentNode.

So you can adjust the DHTML micro API to fit this by adding the below code to the first if block.
this.parent = document.getElementById(name).parentNode;

And, to the second if block…
this.parent = document.all[name].parentElement;

And to the final one…
this.parent = document.layers[name].parentNode;

So your final JavaScript block should look like the below (including the parent addition to the DHTML micro API, with all additions in bold):
<script language="javascript">
function getObj(name)
{
/* DHTML Micro API
* Source: http://www.quirksmode.org/js/dhtmloptions.html
*/
if (document.getElementById) // test if browser supports document.getElementById
{
this.obj = document.getElementById(name);
this.style = document.getElementById(name).style;
this.parent = document.getElementById(name).parentNode;
}
else if (document.all) // test if browser supports document.all
{
this.obj = document.all[name];
this.style = document.all[name].style;
this.parent = document.all[name].parentElement;
}
else if (document.layers) // test if browser supports document.layers
{
this.obj = document.layers[name];
this.style = document.layers[name].style;
this.parent = document.layers[name].parentNode;
}
}

function checkedChanged()
{
var firstCheck = new getObj('chkFirstCheck');
var secondCheck = new getObj('chkSecondCheck');

if (firstCheck.obj.checked == true)
{
secondCheck.obj.disabled = false;
secondCheck.parent.disabled = false;
}
else
{
secondCheck.obj.disabled = true;
secondCheck.parent.disabled = true;
secondCheck.obj.checked = false; // if the secondCheck is disabled, then it should not be checked
}
}
</script>

Note that you do not need to use the QuirksMode DHTML micro API if you don’t want to, you can adjust the above to your needs using just getElementById, which most modern browsers support.

25 Responses to “Enabling/Disabling ASP.NET checkbox using JavaScript”

  1. Amitabh Says:

    HI

    this disables the HTML controls on the form. could we get the happening with a ASP server control. What i neeed, when i click a button the client script disables a asp server control.

    Please send some solution to my problem

  2. Paul Says:

    I had the same problem with ASP radio buttons, but your solution of toggling the form fields parent in Javascript did the trick :)

    Thanks very much mate.

  3. umangi Says:

    very useful.

    Amitabh
    it will work with asp control too. and if u click the javascript is disabling ur checkbox try to avoid postback on buttonclick.

    button.Attributes.Add(“OnClick”,”yourfun() return false;”);

    will avoid unnecessary postback & prevent your checkbox to be disabled

  4. Bob Says:

    thank you! been struggling with this one…finally looked at the HTML and noticed the SPAN tags…your posting worked great!

  5. B Parsons Says:

    I concur with previous post, except I had to set the disabled property on BOTH the checkbox AND the parent span before it would behave correctly. Whatever works!

  6. Joe Says:

    .parentNode was a life saver – thanks

  7. Amitabha Chatterjee Says:

    I would like to know, if we want to make a conditional post back from Java script , how it can be possible?
    Like , If a condition matches, the postback will occour otherwise not.
    if I write the following code
    button.Attributes.Add(”OnClick”,”yourfun() return false;”);
    it will unconditionally by pass the postback , so what will be the modified code?
    Please send me the answar , I am facing problem with it.

    Amitabha

  8. Ninel Says:

    Did anyone get this to work? I’m having a lot of trouble with this javascript.

    If anyone got this working please respond

    Thanks,
    Ninel

  9. luisetxe Says:

    Somehow related with this issue is the way that Firefox and IE interpret the tag.

    I.e. in the code of this blog example

    I’m second!!!

    IE disables ALL the controls inside the tag ( in this case, checkbox and label)
    Firefox, in the other hand, disables only the checkbox, but the label remains “enabled”.

    I tried with other html controls, i.e. a button, and the behavior is the same. IE disables the button, Firefox doesn’t.

    I don’t know is this obvious, but it was new to me. When I disable a checkbox, IE will show it completely grayed out. Firefox won’t gray out the label, but will disable the checkbox itself.

  10. David Says:

    IE is broken in this regard, and ASP.NET even more so in outputting invalid markup. disabled=”disabled” is not valid on tags other than form controls. As the label isn’t part of the tag then the label isn’t grayed out.

  11. Stephen Says:

    Great post! Had the same problem, .Net 2.0 nonetheless. I guess MS doesn’t use client script and checkboxes on their own stuff.

  12. Laurent Says:

    That’s great, but what what happen when the page is posted back?
    That’s a good question because the properties of the checkbox doesn’t seem to be correct server side…

    Any experiences on this are more than welcome.

  13. jmon Says:

    nice javascript *barf* layers? document.all? seriously??

  14. Bigby Says:

    Thank you very much for helping solve my problem, it is much appreciated.

    jmon, you’re a twat.