Add disabled attribute to input element using Javascript
Asked Answered
I

9

182

I have an input box and I want it to be disabled and at the same time hide it to avoid problems when porting my form.

So far I have the following code to hide my input:

$(".shownextrow").click(function() { 
    $(this).closest("tr").next().show().find('.longboxsmall').hide();
});

This is the input that gets hidden as a result:

<input class="longboxsmall" type="text" />

How can I also add the disabled attribute to the input?

Intoxicating answered 27/9, 2010 at 18:29 Comment(0)
A
379

$("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://mcmap.net/q/40503/-prop-vs-attr

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...

Aviary answered 27/9, 2010 at 18:32 Comment(4)
+1 for making the update text large enough for me to pay attentionNanine
@VaelVictus Not so fast. I'm sorry to say they've changed it again a year after I posted this... and I forgot about this answer. Read this answer: https://mcmap.net/q/40503/-prop-vs-attrAviary
That document/non-document differentiation between .prop and .attr are infinitely helpful and I've never seen the context framed in such a succinct manner before. Much appreciated!Leggat
Stills working todaySoelch
G
77

Working code from my sources:

HTML WORLD

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

JS WORLD

var from = jQuery('select[name=select_from]');

//add disabled
from.attr('disabled', 'disabled');



//remove it
from.removeAttr("disabled");
Gustavogustavus answered 20/3, 2014 at 4:38 Comment(1)
As a note for people coming here from Google, directly from jQuery: As of jQuery 1.6, the .attr() method returns undefined for attributes that have not been set. To retrieve and change DOM properties such as the checked, selected, or disabled state of form elements, use the .prop() methodCordes
A
24

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);
Algonquian answered 6/12, 2013 at 18:38 Comment(3)
What about $element.eq(0)[0].disabled = true;? :-PBuckie
Yeah, that's to advanced for me, I don't go that far to improve performance.Algonquian
+1. Although it is a bit redundant. If you're going to be dealing with a NodeList/array of elements it's silly to select them by the index like that. Iteration or simply selecting only the element you need makes more sense.Philpot
P
22
$(element).prop('disabled', true); //true|disabled will work on all
$(element).attr('disabled', true); 
element.disabled = true;
element.setAttribute('disabled', true);

All of the above are perfectly valid solutions. Choose the one that fits your needs best.

Philpot answered 6/12, 2013 at 18:30 Comment(1)
Thank you for providing a solution that doesn't require jQuery.Nimwegen
K
15

Since the question was asking how to do this with JS I'm providing a vanilla JS implementation.

var element = document.querySelector(".your-element-class-goes-here");
// it's a good idea to check whether the element exists
if (element != null && element != undefined) {
  element.disabled = "disabled";
}
Kurys answered 17/7, 2020 at 20:12 Comment(0)
U
13

You can get the DOM element, and set the disabled property directly.

$(".shownextrow").click(function() { 
  $(this).closest("tr").next().show()
          .find('.longboxsmall').hide()[0].disabled = 'disabled';
});

or if there's more than one, you can use each() to set all of them:

$(".shownextrow").click(function() { 
  $(this).closest("tr").next().show()
          .find('.longboxsmall').each(function() {
               this.style.display = 'none';
               this.disabled = 'disabled';
          });
});
Unionism answered 27/9, 2010 at 18:33 Comment(0)
S
6

Just use jQuery's attr() method

$(this).closest("tr").next().show().find('.longboxsmall').attr('disabled', 'disabled');
Swinford answered 27/9, 2010 at 18:33 Comment(1)
and remove disabled state using .removeAttr('disabled');Favorite
D
5

in JQuery you can use the following functions:

$("input").prop('disabled', true);
$("input").prop('disabled', false);

For native js you can use:

document.getElementById("myElement").disabled = true;
Dillion answered 22/4, 2021 at 10:58 Comment(1)
hi, welcome to StackOverflow! umm, could you use code block on it? it will make others easy to read. just put ``` before and after your code.Justice
P
1
$("input").attr('disabled', true);

I tried .prop(), but that did NOT work for me on JQuery 3.7.1

Panhandle answered 17/4, 2024 at 8:42 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.