views:

1527

answers:

5

I am dynamically creating a CheckBox and setting the disabled property like this:

chk.Disabled = false.

This renders the following HTML:

<input type="checkbox" disabled="disabled" .../>

On the click of the checkbox, I have a JS function that tries to enable it by doing a:

//get reference to the checkbox
chk.disabled = false;

This does not work. Any help?

+1  A: 

If your checkbox is disabled, your onclick wont be called

Allen
A: 

How are you dynamically creating the check box?

Keep in mind that ASP.NET will change the name of the check box you give it, especially if you add it on the code behind.

What you need to do is send to your JS function the clientId, which is the id the HTML item will get when render.

David Basarab
+1  A: 

What you're trying to do is a bit odd. You're trying to enable a checkbox by clicking on the checkbox, which is disabled to start with. So, the onclick will not be registered until the checkbox is actually enabled.

Try the below to see what I mean.

<html>
<body>
    <input id="cb" type="checkbox" disabled="disabled" onclick="this.disabled=!this.disabled;" />
    <label for="cb">Click me</label>

    <input type="button" value="Click me instead!" onclick="cb.disabled=!cb.disabled;" />
</body>
</html>

Hope that helps you out.

Antony Scott
A: 

Sorry for the cofusion. I mean on a click of a different checkbox, i am trying to enable the checkbox in question.

I can successfully enable/disable from JS when it is enabled to begin with, but if it's disabled, on clicking a different checkbox, I am not able to enable this checkbox.

A: 

It's still a little unclear what you're trying to do. From what you're said this would seem to be what you're wanting to do ...

<html>
<body>
    <input id="cb" type="checkbox" onclick="cb2.disabled=!cb2.disabled;fs.disabled=!fs.disabled;" />
    <label for="cb">Enable/Disable</label>
    <br />
    <input id="cb2" type="checkbox" disabled="disabled" />
    <label for="cb2">Click me</label>
    <br />
    <input id="cb3" type="checkbox" />
    <label for="cb3">Some other checkbox</label>
</body>
</html>

If not, then perhaps you could include some example html that highlights the problem you're facing.

Antony Scott