How to disable/enable input with JavaScript or jQuery?

I have a form where I want to disable some input fields. Then based on user input I want to enable them.
How do I do that with JavaScript or jQuery?

In JavaScript, you can do as below.

document.getElementById('input-id').disabled = true;
document.getElementById('input-id').disabled = false;

If you are using jQuery then you can use the solution below.

jQuery 1.6+
If the jQuery version is greater than 1.6 then you should use the .prop() function to change the disabled property.

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

jQuery 1.5 and below

The .prop() function doesn’t exist, but you can use .attr() function which does similar.

$("input").attr('disabled','disabled');
$("input").removeAttr('disabled');
If you are trying to disable in any event or you have access to **this** keyword, then you can directly disable the element. It will work for any version of jQuery and it is faster than any other method. Let's give an example.
$("input").change(function(){
     this.disabled = true;
});