0

How do we disabled buttons in javascript/jquery?

I tried:

$('#imgupload').disabled=true;

and

$('#imgupload').attr('disabled','disabled');

but neither works. Right after the above I put a console.log line that works, so I know the surrounding code is fine.

The only thing that disables my button is if I hard code that it's disabled like this:

<input type='submit' id='imgupload' value='Upload' disabled=disabled />

But I need to enable and disable it dynamically on certain events so I can't use that.

user961627
  • 12,379
  • 42
  • 136
  • 210
  • 1
    For the difference between `prop()` and `attr()` see the excellent discussion at [.prop() vs .attr()](http://stackoverflow.com/questions/5874652/prop-vs-attr) – Boaz Jan 28 '13 at 08:09

3 Answers3

1

Grab DOM element here:

$('#imgupload')[0].disabled = true;
              --^-- // DOM element

Or use prop:

$('#imgupload').prop('disabled', true);
elclanrs
  • 92,861
  • 21
  • 134
  • 171
1

You should be using prop() :

$('#imgupload').prop('disabled', true);
adeneo
  • 312,895
  • 29
  • 395
  • 388
1

prop() should do

$('#imgupload').prop('disabled', true);
bipen
  • 36,319
  • 9
  • 49
  • 62