10

Input elements can be easily disabled but what way is there to enable them?

The below code can disable but not enable the input element.

$('#dis').click(function() { $('#inp').attr('disabled', 'true'); }); $('#enb').click(function() { $('#inp').attr('disabled', 'false'); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <input id='inp' type='text'> <button id='enb'>Enable</button> <button id='dis'>Disable</button>

1
  • 2
    Do not user $().attr() user $().prop() instead. See api.jquery.com/prop Commented Sep 26, 2015 at 10:05

8 Answers 8

8

Just to expand on the answer...

The real problem is that you are trying to set disabled to false via setAttribute() which doesn't behave as you are expecting. An element is disabled if the disabled-attribute is set, regardless of value so, disabled="true", disabled="disabled" and disabled="false" are all equivalent: the element gets disabled).

You should instead remove the complete attribute

$('#enb').click(function() { $('#inp').removeAttr('disabled'); }); 
Sign up to request clarification or add additional context in comments.

Comments

6

The issue was with your code having true and false written in quotes, making them strings instead of booleans. Note that both 'true' and 'false' are truthy values, and thus your code set the enabled value to true in both cases.

I expect the following will work for you, check the snippet as well:

$('#dis').click(function() { $("#inp").prop('disabled', true); }); $('#enb').click(function() { $("#inp").prop('disabled', false); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <input id='inp' type='text'> <button id='enb'>Enable</button> <button id='dis'>Disable</button>

Comments

4

Just remove the disabled attribute

$('#enb').click(function() { $('#inp').removeAttr('disabled'); }); 

5 Comments

This is not a good practice. from the documentation: Do not use this method to remove native properties such as checked, disabled, or selected.
why not? any insights @NitaiJ.Perez?
@anurageldorado, the docs mention it may remove the prop/attr permanently, not allowing it to be re-set.
So it's not so harmful in all the cases right?
@anurageldorado, why take the risk? See my answer for a safe method.
2

You can use removeAttr()

$('#dis').click(function() { $('#inp').attr('disabled', 'true'); }); $('#enb').click(function() { $('#inp').removeAttr('disabled'); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <input id='inp' type='text'> <button id='enb'>Enable</button> <button id='dis'>Disable</button>

Comments

1
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <input id='inp' type='text'> <button id='enb'>Enable</button> <button id='dis'>Disable</button> <script type="text/javascript"> $( document ).ready(function() { $( "#dis" ).click(function() { $('#inp').prop( "disabled", true ); }); $( "#enb" ).click(function() { $('#inp').prop( "disabled", false ); }); }); </script> 

Comments

1

Hi Try the below code

There is different between Boolean values and strings instead of 'true' use true without quotes

$('#dis').click(function() { $('#inp').attr('disabled', true); }); $('#enb').click(function() { $('#inp').attr('disabled', false); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <input id='inp' type='text'> <button id='enb'>Enable</button> <button id='dis'>Disable</button>

5 Comments

Again, the attr() Method does not work with boolean values
@Lacrioque who is saying?
Jquey online documentation: api.jquery.com/prop See Attributes versus Properties
Answer for what you said about using Boolean values..
Pre 1.6 Jquery used boolean values with attr() I give you that, but after the prop() Method got introduced, the documentation clearly states, that using boolean values with attr() will cause errors. If you have a working code great! But again boolean values should not be used with attr()
0

try

$('#dis').click(function() { $('#inp').attr('disabled', 'true'); }); $('#enb').click(function() { $('#inp').removeAttr('disabled'); }); 

because disable="disable" is equal to disable itself

Comments

0

There is a way! You need to remove the disabled attribute like so :

$('#inp').removeAttr('disabled'); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.