Search code examples
javascripthtmldisabled-input

How to prevent an input from being disabled?


I have an input field <input> with id my-field to which some plugin adds the attribute disabled="disabled" after the first escape.

This is a problem so i'm trying to keep that disabled attribute away from it with the code below:

var input = document.getElementById("my-field");
document.addEventListener("change", input, function(){
    setTimeout(function() {
        input.disabled = false;
    }, 50)
});

but it's not working, what am I doing wrong?

EDIT: as suggested below i corrected the boolean to have no "" but it's not working.


Solution

  • Try to redefine the setAttribute method and disabled property in the input, something like:

    function removeDisabled(input) {
        input.disabled = false;
        input.removeAttribute('disabled');
        Object.defineProperties(input, {
            disabled: {
                get: function() { return false; }
            },
            setAttribute: {
                value: function() {}
            }
        })
    }
    
    removeDisabled(document.getElementById('my-field'))