Tuesday, 26 January 2016

Disable all fields on a Web form (ASP.net, HTML forms)

Hi,
 
Today we got a requirement to disable all the fields on the ASP.net form based on some conditions.
 
This can be easily done using JavaScript. Here is the code for it.

var inputs = document.getElementsByTagName("input");
for (var i = 0; i < inputs.length; i++) {
     // For applying background color and color.
     // inputs[i].style.backgroundColor = "#9fd4fe";
     // inputs[i].style.color = "black";
     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;
}
var links = document.getElementsByTagName('a');
for (var i = 0; i < links.length; i++) {
     links[i].disabled = true;
}

Hope this helps.
 
--
Happy Coding

Gopinath

No comments:

Post a Comment