Luci
Luci

Reputation: 3264

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

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?

Upvotes: 66

Views: 136972

Answers (16)

Marco Concas
Marco Concas

Reputation: 1892

Based on the answer by Tim Down, I suggest a slight modification:

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.

Upvotes: -1

Rafael
Rafael

Reputation: 63

You could do something like:

const form = document.querySelector("form");
const allFormChildren = form.querySelectorAll("*");
allFormChildren.forEach(c => {
  if ("disabled" in c) c.disabled = true;
})

This would disable all form elements that can be disabled.

Upvotes: 0

Wojciechu
Wojciechu

Reputation: 3910

Old question, but nobody mentioned using css:

pointer-events: none;

Whole form becomes immune from click but also hovers.

To make it not possible to tab through, add tabindex="-1" to the input elements.

Upvotes: 7

TV-C-1-5
TV-C-1-5

Reputation: 714

I like this method but with the 'disabled' property:

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

That way all form fields, radios, checkboxes, text fields, text areas - and including hidden form fields - are all disabled at once.

This is very handy if you are switching between forms.

Upvotes: 0

Brennan
Brennan

Reputation: 359

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);

Upvotes: 3

Avinash Raut
Avinash Raut

Reputation: 2113

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;
        }    
    }

Upvotes: -1

Andr&#233;s Lescano
Andr&#233;s Lescano

Reputation: 1

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.

Upvotes: -3

nyandereian
nyandereian

Reputation: 79

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.

Upvotes: 6

Dan Hargis
Dan Hargis

Reputation: 457

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);
	});

Upvotes: -1

Umar Asghar
Umar Asghar

Reputation: 4044

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.

Upvotes: 4

Shanu
Shanu

Reputation: 109

Its Pure Javascript :

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

Upvotes: 10

SHS
SHS

Reputation: 149

// 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;
}

Upvotes: -1

Nick Craver
Nick Craver

Reputation: 630429

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.

Upvotes: 45

piotr_cz
piotr_cz

Reputation: 9606

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

Upvotes: 59

Māris Kiseļovs
Māris Kiseļovs

Reputation: 17295

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

or to make it read-only:

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

Upvotes: 1

Tim Down
Tim Down

Reputation: 324587

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;
}

Upvotes: 57

Related Questions