6

I am trying using several asp.net checkboxes on a page, disabling them accordingly.

<asp:CheckBox ID='chkMenuItem' runat='server' CssClass='HiddenText' Text='Test'      onclick='<%#String.Format("checkChild({0});", Eval("id")) %>' />

on javascript, I am using the following code

function checkChild(id) {
            for (i = 0; i < $("input[id*=hdnParentMenuItemID]").length; i++) {
                if ($('input[id*=hdnParentMenuItemID]')[i].value.split(':')[0] == id) {
                    var childID = $('input[id*=hdnParentMenuItemID]')[i].value.split(':')[1];
                    if ($("#" + childID).attr("disabled"))
                    //$("#" + childID).attr('disabled', '');
                        $("#" + childID).removeAttr("disabled");
                    else
                        $("#" + childID).attr('disabled', true);
                }
            }
        }

Now is the checkboxes are disabled once the page is loaded, the removeAttr section doesn't work. I tried to step through the debugger and the logic works perfectly fine. If the checkboxes aren't disabled on page load, the code works fine. I tried replacing disabled 'attributes' with 'checked' to see if the other attributes work fine and it works perfectly fine. I tried

 $("#" + childID).attr('disabled', '');

but it didnt work either.

Note: It works perfect on FF and Chrome but doesnt work in IE.

Thanks,

1
  • Please see artzstudio.com/2009/04/jquery-performance-rules/… to clean up your for loop. Your making your script unnecessarily slow by doing this check i < $("input[id*=hdnParentMenuItemID]").length . Put $("input[id*=hdnParentMenuItemID]") into a local variable and also put the length property into a local variable. Commented Dec 4, 2009 at 16:34

4 Answers 4

13

I had similar problems enabling an <asp:CheckBox /> in Internet Explorer using jQuery. The following code worked perfectly fine in FireFox.

$('myCheckBox').removeAttr('disabled');

However, it failed to work properly in IE.

An <asp:CheckBox /> is rendered as a <span /> with an <input /> and a <label /> tag. When the checkbox is disabled both the span and the input tags are decorated with the disabled attribute. To get the expected behavior I used to the following code:

$('myCheckBox').removeAttr('disabled');
$('myCheckBox').closest('span').removeAttr('disabled');
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the solution! I was really irked at IE for this behavior, until I figured out why they place the disabled attribute on the span tag, which is because it makes it more obvious that the checkbox is disabled if the text accompanying it is grayed-out as well.
0

This should def. work

$('#' + childID)[0].disabled = false;

3 Comments

This will not work. As long as the disabled expando property has a value or the disabled attribute has a value, it will remain disabled. e.g. <input type="button" disabled="false" value="I'm still disabled">. You need to explicilty remove the disabled attribute. See Joel Potter's answer.
$('#' + childID)[0] is a reference to an actual DOM element, right? If so, setting the disabled property will certainly work. No need to do anything to the attribute.
@Tim Down: it is indeed. @nickyt: javascript handles properties like disabled and checked natively like this. the properties and their values are not a simple 1:1 mapping to the HTML attributes.
0

You want an if statement like this

if ($("#" + childID + ":disabled").length) // is childID disabled?
    $("#" + childID).removeAttr("disabled"); // enable it
else
    $("#" + childID).attr('disabled', 'disabled'); // disable it

Comments

0

There are loads of redundant selectors here that will be making this function very inefficient. First, running queries for selectors is very slow relative to the rest of the code, so cache the result of your selector and work on that. Also, you should declare i with the var operator to make it local to your function. And there's no need for jQuery to get an element by its ID or that deeply confusing stuff to get and set its disabled status, which can be done far more simply with the boolean DOM disabled property. All that said, I don't really know why your code's not working, but making it readable and simple will definitely help.

function checkChild(id) {
    var input, checkBox, parts, inputs = $("input[id*=hdnParentMenuItemID]");
    for (var i = 0, len = inputs.length; i < len; i++) {
        input = inputs[i];
        parts = input.value.split(':');
        if (parts[0] == id) {
            checkBox = document.getElementById( parts[1] );
            checkBox.disabled = !checkBox.disabled;
        }
    }
}

3 Comments

I think you mean input = inputs[i]
Wouldn't using $(...).each(...) be cleaner than the for loop?
@Nick Craig-Wood: Probably: a few characters fewer and a little neater, but also slower because of the extra function call each iteration. Fact is that I don't use jQuery so I don't know off the top of my head whether what gets passed to the function called each time round in each in jQuery is a DOM element or some kind of jQueryfied thingy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.