Below generic code used to clear the form fields in asp or asp.net webpage. This code is written in JavaScript to avoid postback to server.
JavaScript Code:
function ClearAllFields() {
for (var count = 0; count < document.forms.length; count++) { for (var eleCount = 0; eleCount < document.forms[count].elements.length; eleCount++) { if (document.forms[count].elements[eleCount].type == ‘text’) { document.forms[count].elements[eleCount].value = ”; document.forms[count].elements[eleCount].readOnly = false; } else if (document.forms[count].elements[eleCount].type == ‘checkbox’) { document.forms[count].elements[eleCount].checked = false; document.forms[count].elements[eleCount].disabled = false; } else if (document.forms[count].elements[eleCount].type == ‘select-one’) { document.forms[count].elements[eleCount].selectedIndex = 0; document.forms[count].elements[eleCount].disabled = false; } else if (document.forms[count].elements[eleCount].type == ‘textarea’) { document.forms[count].elements[eleCount].value = ”; document.forms[count].elements[eleCount].readOnly = false; } else if (document.forms[count].elements[eleCount].type == ‘select-multiple’) { document.forms[count].elements[eleCount].selectedIndex = -1; document.forms[count].elements[eleCount].disabled = false; } } } return false; }
Advertisement