0

This code does not disable my element as expected

if(Multifile.n==0){
    $("#btnUpload").attr("disabled","disabled");
}

This code does not enable my element as expected

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

I added these lines in jQuery.MultiFile.js after what I believe is the location where files are added and removed from the MultiFile object. As far as I can tell, MultiFile.n is a counter.

How can I disable btnUpload when there are no files in the list, and enable it as long as there is at least one.

David Fox
  • 10,603
  • 9
  • 50
  • 80
  • Not *answering* because I don't know anything about `Multifile`, but your second example won't enable the element regardless. To undo `.attr("disabled", "disabled")`, you need to use `.removeAttr("disabled")`. Having the `disabled` attribute with the value "" still disables the element. Details: http://www.w3.org/TR/html5/infrastructure.html#boolean-attribute – T.J. Crowder May 12 '10 at 14:12
  • And to disable an element, `.attr('disabled', true)` works fine - and it's clearly not as awful as setting the value to 'disabled' – ThiefMaster May 12 '10 at 14:17
  • .removeAttr("disabled"); does not work either. I also removed the initial disabled attribute and it remains enabled regardless of the number of files – David Fox May 12 '10 at 14:28
  • i found via Glyphix debugger that the counter was actually at 2 by the time the document loads. I changed my condition to match and it works to toggle. however, the button does not seem to initialize as disabled – David Fox May 12 '10 at 16:13

1 Answers1

0

If there is a disabled attribute on the element, it will always be disabled, even if the attribute is empty. This is because the original (SGML variant of) HTML had attributes without values like this

<INPUT TYPE="text" DISABLED>

So you have to remove the attribute

$('#btnUpload').removeAttr('disabled');
Jan Willem B
  • 3,787
  • 1
  • 25
  • 39
  • I removed the initial attribute, and it still never changes – David Fox May 12 '10 at 14:30
  • then probably the button involved is not the element with the btnUpload id. Have you checked that with the google chrome element inspector? (right click -> inspect element) – Jan Willem B May 12 '10 at 14:49
  • it is definitely the element with id (and name attribute) equal to btnUpload. should I check something other than page source? – David Fox May 12 '10 at 15:04
  • i used firebug `` – David Fox May 12 '10 at 15:10
  • i mentioned above that i discovered the count was actually at 2 when there were no files in the list. i've also confirmed that the location of my toggle code is good. however, i can't get the button to initialize disabled unless i set the `` in which case it remains disabled forever. – David Fox May 12 '10 at 17:56