how can I disable everything inside a form using javascript/jquery?

JavascriptJqueryReadonly

Javascript Problem Overview


I have a form that pop up inside a layer, and I need to make everything inside that form read only regarding what type of input it is. Anyway to do so?

Javascript Solutions


Solution 1 - Javascript

This is quite simple in plain JavaScript and will work efficiently in all browsers that support read-only form inputs (which is pretty much all browsers released in the last decade):

var form = document.getElementById("your_form_id");
var elements = form.elements;
for (var i = 0, len = elements.length; i < len; ++i) {
    elements[i].readOnly = true;
}

Solution 2 - Javascript

With HTML5 it's possible to disable all inputs contained using the <fieldset disabled /> attribute.

> disabled: > > If this Boolean attribute is set, the form controls that are its > descendants, except descendants of its first optional > element, are disabled, i.e., not editable. They won't received any > browsing events, like mouse clicks or focus-related ones. Often > browsers display such controls as gray.

Reference: MDC: fieldset

Solution 3 - Javascript

You can use the :input selector, and do this:

$("#myForm :input").prop('readonly', true);

:input selects all <input>, <select>, <textarea> and <button> elements. Also the attribute is readonly, if you use disabled to the elements they won't be posted to the server, so choose which property you want based on that.

Solution 4 - Javascript

Its Pure Javascript :

var fields = document.getElementById("YOURDIVID").getElementsByTagName('*');
for(var i = 0; i < fields.length; i++)
{
	fields[i].disabled = true;
}

Solution 5 - Javascript

Old question, but nobody mentioned using css:

pointer-events: none;

Whole form becomes immune from click but also hovers.

Solution 6 - Javascript

You can do this the easiest way by using jQuery. It will do this for all input, select and textarea elements (even if there are more than one in numbers of these types).

$("input, select, option, textarea", "#formid").prop('disabled',true);

or you can do this as well but this will disable all elements (only those elements on which it can be applied).

$("*", "#formid").prop('disabled',true);


disabled property can applies to following elements:

  • button
  • fieldset
  • input
  • optgroup
  • option
  • select
  • textarea

But its upto you that what do you prefer to use.

Solution 7 - Javascript

Old question, but right now you can do it easily in pure javascript with an array method:

form = document.querySelector('form-selector');
Array.from(form.elements).forEach(formElement => formElement.disabled = true);
  1. form.elements returns a collection with all the form controls (inputs, buttons, fieldsets, etc.) as an HTMLFormControlsCollection.

  2. Array.from() turns the collection into an array object.

  3. This allows us to use the array.forEach() method to iterate through all the items in the array...

  4. ...and disable them with formElement.disabled = true.

Solution 8 - Javascript

$("#formid input, #formid select").attr('disabled',true);

or to make it read-only:

$("#formid input, #formid select").attr('readonly',true);

Solution 9 - Javascript

Here is another pure JavaScript example that I used. Works fine without Array.from() as a NodeList has it's own forEach method.

document.querySelectorAll('#formID input, #formID select, #formID button, #formID textarea').forEach(elem => elem.disabled = true);

Solution 10 - Javascript

// get the reference to your form 
// you may need to modify the following block of code, if you are not using ASP.NET forms  
var theForm = document.forms['aspnetForm'];
if (!theForm) {
 theForm = document.aspnetForm;
}

// this code disables all form elements  
var elements = theForm.elements;
for (var i = 0, len = elements.length; i < len; ++i) {
 elements[i].disabled = true;
}

Solution 11 - Javascript

This one has never failed me and I did not see this approach on the other answers.

//disable inputs
$.each($("#yourForm").find("input, button, textarea, select"), function(index, value) {
		$(value).prop("disabled",true);
	});

Solution 12 - Javascript

disable the form by setting an attribute on it that disables interaction generally

<style>form[busy]{pointer-events:none;}</style>
<form>....</form>
<script>
function submitting(event){
    event.preventDefault();
    const form = this; // or event.target;
    // just in case...
    if(form.hasAttribute('busy')) return;
    // possibly do validation, etc... then disable if all good
    form.setAttribute('busy','');
    
    return fetch('/api/TODO', {/*TODO*/})
    .then(result=>{ 'TODO show success' return result; })
    .catch(error=>{ 'TODO show error info' return Promise.reject(error); })
    .finally(()=>{
        form.removeAttribute('busy');
    })
    ;  
}
Array.from(document.querySelectorAll('form')).forEach(form=>form.addEventListener('submit',submitting);
</script>

Solution 13 - Javascript

Javascript : Disable all form fields :

function disabledForm(){
  var inputs = document.getElementsByTagName("input"); 
    for (var i = 0; i < inputs.length; i++) { 
        inputs[i].disabled = true;
    } 
     var selects = document.getElementsByTagName("select");
     for (var i = 0; i < selects.length; i++) {
         selects[i].disabled = true;
     }
    var textareas = document.getElementsByTagName("textarea"); 
    for (var i = 0; i < textareas.length; i++) { 
        textareas[i].disabled = true;
    }
    var buttons = document.getElementsByTagName("button");
    for (var i = 0; i < buttons.length; i++) {
        buttons[i].disabled = true;
    }    
}

To Enabled all fields of form see below code

function enableForm(){
      var inputs = document.getElementsByTagName("input"); 
        for (var i = 0; i < inputs.length; i++) { 
            inputs[i].disabled = false;
        } 
         var selects = document.getElementsByTagName("select");
         for (var i = 0; i < selects.length; i++) {
             selects[i].disabled = false;
         }
        var textareas = document.getElementsByTagName("textarea"); 
        for (var i = 0; i < textareas.length; i++) { 
            textareas[i].disabled = false;
        }
        var buttons = document.getElementsByTagName("button");
        for (var i = 0; i < buttons.length; i++) {
            buttons[i].disabled = false;
        }    
    }

Solution 14 - Javascript

As the answer by Tim Down I suggest:

const FORM_ELEMENTS = document.getElementById('idelementhere').elements;
for (i = 0; i < FORM_ELEMENTS.length; i++) {
    FORM_ELEMENTS[i].disabled = true;
}

This will disable all elements inside a form.

Solution 15 - Javascript

for what it is worth, knowing that this post is VERY old... This is NOT a read-only approach, but works for me. I use form.hidden = true.

Solution 16 - Javascript

Thanks Tim,

That was really helpful. I have done a little tweaking when we have controls and we handle a event on them.

 var form = document.getElementById("form");
                var elements = form.elements;
                for (var i = 0, len = elements.length; i < len; ++i) {
                    elements[i].setAttribute("onmousedown", "");
                }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLuciView Question on Stackoverflow
Solution 1 - JavascriptTim DownView Answer on Stackoverflow
Solution 2 - Javascriptpiotr_czView Answer on Stackoverflow
Solution 3 - JavascriptNick CraverView Answer on Stackoverflow
Solution 4 - JavascriptShanuView Answer on Stackoverflow
Solution 5 - JavascriptWojciechuView Answer on Stackoverflow
Solution 6 - JavascriptUmar AsgharView Answer on Stackoverflow
Solution 7 - JavascriptnyandereianView Answer on Stackoverflow
Solution 8 - JavascriptMāris KiseļovsView Answer on Stackoverflow
Solution 9 - JavascriptBrennanView Answer on Stackoverflow
Solution 10 - JavascriptSHSView Answer on Stackoverflow
Solution 11 - JavascriptDan HargisView Answer on Stackoverflow
Solution 12 - JavascriptjimmontView Answer on Stackoverflow
Solution 13 - JavascriptAvinash RautView Answer on Stackoverflow
Solution 14 - JavascriptMarco ConcasView Answer on Stackoverflow
Solution 15 - JavascriptAndrés LescanoView Answer on Stackoverflow
Solution 16 - JavascriptSudeep nayakView Answer on Stackoverflow