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?
Just use jQuery's
attr()
methodIf you're using jQuery then there are a few different ways to set the disabled attribute.
Working code from my sources:
HTML WORLD
JS WORLD
$("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:
Or in other words:
... ...
May we all learn a lesson here about API stability...
You can get the DOM element, and set the disabled property directly.
or if there's more than one, you can use
each()
to set all of them:All of the above are perfectly valid solutions. Choose the one that fits your needs best.