Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 790
Luci Avatar asked Aug 02 '10 10:08

Luci


People also ask

How do I disable all elements in a form?

To disable all form elements inside 'target', use the :input selector which matches all input, textarea, select and button elements.

How do I remove all fields in form?

To clear all the input in an HTML form, use the <input> tag with the type attribute as reset.

How disable all controls inside a div using jQuery?

Answer: To disable all input elements within div use the following code: $('#message :input'). attr('disabled', true);

What is the correct syntax to make element disable in a form in JavaScript?

$( "#x" ).prop( "disabled", false );


2 Answers

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;
}
like image 113
Tim Down Avatar answered Oct 22 '22 10:10

Tim Down


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

like image 49
piotr_cz Avatar answered Oct 22 '22 11:10

piotr_cz