disable all the elements in html

后端 未结 8 1234
清歌不尽
清歌不尽 2020-12-14 09:13

How can we disable all the elements in html through javascript.The easiest way...

8条回答
  •  生来不讨喜
    2020-12-14 09:31

    Just and without crutches!

    /**
     * Enable/disable all form controlls
     * @param status Status: true - form active, false - form unactive
     */
    HTMLFormElement.prototype.setStatus = function (status) {
        for (var i in this.elements) {
            this.elements[i].disabled = !status;
        }
    };
    
    // Example:
    var my_form = document.getElementById('my_form_with_many_inputs');
    my_form.setStatus(false); // Disable all inputs in form
    my_form.setStatus(true); // Enable all inputs in form
    

提交回复
热议问题