views:

108

answers:

4

I have this function:

function disableDiv(divId, action){
    var divId = byId(divId);

    if(action==true){
    divId.style.display='none';
    }
    else if(action==false){
    divId.style.display='block';
    }

    var inputs = divId.getElementsByTagName("input");
    var selects = divId.getElementsByTagName("select");
    var i;

    for (i=0; i<inputs.length; i++){
        inputs[i].disabled=action;
        }

    for (i=0; i<selects.length; i++){
        selects[i].disabled=action;
        }
}

This takes a divId (id of DIV) and an action (false or true) and gets all inputs and selects inside the div, and sets their disabled attribute to either false or true.

According to Firebug, the elements inside the Div are disabled all the time. But they should be active once hitting a drop-list option... The triggering is fine so you know. I can see this function beeing called by using alert boxes, and it does in fact set the disabled=false. But the elements are still disabled.

Something to point out is that according to firebug, the disabled attribute looks like this:

    <input name="test" id="test" disabled="">

Note there is just two doublequotes... Shouldn't it say "disabled='disabled'" or "disabled=true"?

Any tips on how to troubleshoot further?

Here is how I call the function:

(category=="Cars")?disableDiv("nav_sub_cars", false):disableDiv("nav_sub_cars", true);

If you need more input, just let me know...

Thanks

+4  A: 

Edited to reflect the comments.

According to the W3C the code you posted should be correct. The disabled attribute is a boolean attribute. Use of the removeAttribute() method may be helpful as well.

In my experience, you can also achieve this effect using the string values 'disabled' or ''. This may work because these values are coerced into a boolean representation when the browser encounters them.

g.d.d.c
??? I have a similiar function which disables some inputs by hitting radio button. When hittin radio 1, all elements are disabled, but when hitting radio 2, all are enabled again. It works just fine. Care to explain that?
Camran
Different entities might perform in different ways, but the proper standard compliant HTML says that you should declare an element disabled as disabled="disabled"
Tom Gullen
Regarding the [HTML 4.01](http://www.w3.org/TR/html401/interact/forms.html#adef-disabled) and [HTML5](http://www.whatwg.org/specs/web-apps/current-work/multipage/association-of-controls-and-forms.html#enabling-and-disabling-form-controls) specs the `disabled` attribute is described as a `boolean` attribute, where the presence of a boolean attribute on an element represents the true value, and the absence of the attribute represents the false value.
CMS
I think this refers to another answer I gave where I used true/false. The strange thing is using true/false worked fine in FF and IE6. But using "disabled" will indeed be better.
Castrohenge
Kieranmaine: the string "disabled" is incorrect for the `disabled` property. It should be Boolean `true` or `false`. See CMS's comment.
Tim Down
Tom Gullen: either `disabled="disabled"` or just `disabled` are valid attributes for inputs in HTML.
Tim Down
Why is this marked as the correct answer? It's wrong.
Tim Down
All very interesting. Thanks Tim.
Castrohenge
@Tim Down - I've revised the answer in a way that I believe reflects your comments. Can you help explain why the code the user posted initially, which adheres to the W3C, does not work (the reason for the post)?
g.d.d.c
g.d.d.c: The code posted initially looks correct. There must be some other problem to do with the rest of the set-up that we haven't seen. There is a general issue that a lot of people seem confused about the relationship between attributes and properties, a confusion that I think is exacerbated by jQuery's `attr()` method, which in the majority of cases sets a property, not an attribute. In general, because of incorrect behaviour in IE and for simplicity, you're better off forgetting about attributes in JavaScript and just using properties.
Tim Down
I think you're (still) confusing the `disabled` HTML **attribute** and the `disabled` **property** you get/set in JavaScript using `element.disabled [= (true|false)]`. Also see [MSDN](http://msdn.microsoft.com/en-us/library/ms533732%28VS.85%29.aspx).
Marcel Korpel
A: 

try .disabled = null or .removeAttribute('disabled'). My understanding is that it's the presence or absence of the disabled attribute that governs disabledness, not its value.

Stuart
A: 

To disable elements you need to use attribute disabled = "disabled" rather than true or false. To make it enabled again, you need to remove the disabled attribute. Modify your code like this:

for (i=0; i<inputs.length; i++){
  if (action === false) {
    inputs[i].removeAttribute('disabled');
  }
  else {
    inputs[i].setAttribute('disabled', 'disabled');
  }
}

for (i=0; i<selects.length; i++){
  if (action === false) {
    selects[i].removeAttribute('disabled');
  }
  else {
    selects[i].setAttribute('disabled', 'disabled');
  }
}

The setAttribute and removeAttribute functions are used to set and remove disabled attribute respectively.

Sarfraz
This is equivalent to the posted code.
Ms2ger
@Marcel Korpel: WAM: How is it useless? And what is the problem with `action === true/false`???
Sarfraz
@ms2ger: You people are wasting my time. It is not never equivalent ! OP was setting `disabled` to true or false which was wrong. It can be set by `disabled="disabled"` or removing the `disabled` attribute something I am doing with `setAttribute` and `removeAttribute`.
Sarfraz
This code, while excessively verbose, is correct - while the 'correct' marked answer is not, so I'm not sure how it ended up with a -1.
Stuart
Stuart: this answer may be correct according to the DOM standard (apart from the copy/paste error in the first section), but you're much better off using the `disabled` property instead.
Tim Down
+2  A: 

More code needed. Everything looks correct, and setting the disabled property of an <input> element to a Boolean value (the correct approach) certainly works in Firefox, regardless of the presence or absence of the disabled attribute in the source HTML.

Tim Down