Enable and disable item with jQuery

7
  

From the original OS question Disable / enable an input with   jQuery?

I want to enable and disable elements of an HTML page (be it inputs, divs, buttons, etc.).

How can I solve it using jQuery?

    
asked by Joacer 01.08.2017 в 09:24
source

1 answer

9
  

From the response of the original OS Disable / enable an input with jQuery?

jQuery 1.6 +

To change the disabled property you must use the .prop() function.

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

jQuery 1.5 and lower

The .prop() function does not exist, but .attr() works similarly:

Set the disabled attribute:

$("input").attr('disabled','disabled');

To enable again, the appropriate method is to use .removeAttr()

$("input").removeAttr('disabled');

In any version of jQuery

You can always trust the current DOM object and it's probably a little faster than the other two options if it's just an element:

// assuming an event handler thus 'this'
this.disabled = true;

The advantage of using the .prop () or .attr () methods is that you can set the property for a bunch of selected items.

Note: In 1.6 there is a method .removeProp() that sounds a lot like removeAttr() , but < strong> MUST NOT BE USED in native properties as 'disabled' Excerpt from documentation:

  

Note: Do not use this method to remove native properties such as checked, disabled, or selected. This will remove the property completely and, once removed, can not be added again to element. Use .prop () to set these properties to false instead.

Translation :

  

Note: Do not use this method to remove native properties, such as   checked, disabled or selected. This will completely eliminate the   property and, once deleted, can not be added back to the   element. Use .prop () to set these properties to false in   your place.

In fact, I doubt there are many legitimate uses for this method, Boolean properties are made in such a way that you must set them to false instead of "delete" them as their "attribute" equivalents in 1.5

    
answered by 01.08.2017 / 09:24
source