Add disabled attribute to input element using Javascript Add disabled attribute to input element using Javascript javascript javascript

Add disabled attribute to input element using Javascript


$("input").attr("disabled", true); as of... I don't know any more.

It's December 2013 and I really have no idea what to tell you.

First it was always .attr(), then it was always .prop(), so I came back here updated the answer and made it more accurate.

Then a year later jQuery changed their minds again and I don't even want to keep track of this.

Long story short, as of right now, this is the best answer: "you can use both... but it depends."

You should read this answer instead: https://stackoverflow.com/a/5876747/257493

And their release notes for that change are included here:

Neither .attr() nor .prop() should be used for getting/setting value. Use the .val() method instead (although using .attr("value", "somevalue") will continue to work, as it did before 1.6).

Summary of Preferred Usage

The .prop() method should be used for boolean attributes/properties and for properties which do not exist in html (such as window.location). All other attributes (ones you can see in the html) can and should continue to be manipulated with the .attr() method.

Or in other words:

".prop = non-document stuff"

".attr" = document stuff

......

May we all learn a lesson here about API stability...


Working code from my sources:

HTML WORLD

<select name="select_from" disabled>...</select>

JS WORLD

var from = jQuery('select[name=select_from]');//add disabledfrom.attr('disabled', 'disabled');//remove itfrom.removeAttr("disabled");


If you're using jQuery then there are a few different ways to set the disabled attribute.

var $element = $(...);    $element.prop('disabled', true);    $element.attr('disabled', true);     // The following do not require jQuery    $element.get(0).disabled = true;    $element.get(0).setAttribute('disabled', true);    $element[0].disabled = true;    $element[0].setAttribute('disabled', true);