views:

58

answers:

4

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?

A: 
$('#formId input').attr('disabled', true);
Victor
Read-only is not the same as disabled.
Tim Down
Yes, you are correct. The original question has 'disable' in the title. Either way, it's trivially easy.
Victor
A: 
$("#formid input, #formid select").attr('disabled',true);

or to make it read-only:

$("#formid input, #formid select").attr('readonly',true);
Māris Kiseļovs
Read-only is not the same as disabled.
Tim Down
@Tim Down, ok i updated my answer for read-only.
Māris Kiseļovs
+4  A: 

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

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

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

Nick Craver
+2  A: 

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