How to disable or make readonly all controls in a page


To make all controls as disable or read-only

Description



Sometimes you might be need to make all textboxes or checkbox as readonly or disable.For example if you check a checkbox in a page then all the textbox should be disabled or readonly.

Use this code



<script language=javascript>
function check() {

for (var i = 0; i < document.forms[0].elements.length; i++) {
var element = document.forms[0].elements[i];
switch (element.type) {
case 'text'://textbox type
document.forms[0].elements[i].readOnly = true;
break;
case 'select-one'://dropdown
document.forms[0].elements[i].readOnly = true;
document.forms[0].elements[i].disabled = true;
break;

case 'checkbox'://checkbox
document.forms[0].elements[i].disabled = true;
break;

case 'radio'://radiobutton
document.forms[0].elements[i].disabled = true;
break;

case 'button'://button

document.forms[0].elements[i].disabled = true;

break;


case 'submit'://button

document.forms[0].elements[i].disabled = true;

break;

case 'textarea'://textarea
document.forms[0].elements[i].readOnly = true;
break;
}
}
}

</script>


You can disable all kind of controls according to your need that just need some modification and you also make the controls as disable as well as readonly.


Comments

Author: macxima16 Feb 2010 Member Level: Gold   Points : 0

Thanks and it is very useful for me.

Author: Jayendra Kumar18 Feb 2010 Member Level: Gold   Points : 0

Thanks to share such a good information with us



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: