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
  • 11,879
  • 38
  • 130
  • 208
  • 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
  • 89,567
  • 21
  • 132
  • 165
1

You should be using prop() :

$('#imgupload').prop('disabled', true);
adeneo
  • 303,455
  • 27
  • 380
  • 377
1

prop() should do

$('#imgupload').prop('disabled', true);
bipen
  • 35,563
  • 9
  • 45
  • 62