![]() |
Web Development »
ASP.NET »
General
License: The Code Project Open License (CPOL)
ResetControlBy Samrat BanerjeeReset all controls using JavaScript function or bring control back in original state using JavaScript |
Javascript, .NET
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
This is a very common requirement that always comes across while building a form. Suppose we have lots of controls such as text box, drop down list, radio button, check box, and file uploader control. If you want to reset them on single click, what will be your approach? Obviously the choice is to clear each field from JavaScript function or you can do this server side as well.
Here I am sharing an important JavaScript function, which can bring them back in the original state. You just need to call the junction in the client click. The most important thing about this function is that it can reset the fileuploader control as well.
Salient features:
RequiredFieldValidater, etc. Just copy the JavaScript function on your page and then simply call the JS function on button onclick event and function reset all types of controls on your page.
And for reset validation controls, you just set your validator control prefix on variable 'validationControlsPrefix'.
For more details, please refer to the uploaded code:
<script type="text/javascript">
//function for reset controls
function ClearAllControls()
{
//set your validation controls prefix here.
var validationControlsPrefix = "req";
resetmsg(validationControlsPrefix);
for (i=0; i<document.forms[0].length; i++)
{
doc = document.forms[0].elements[i];
switch (doc.type)
{
case "text" :
doc.value = "";
break;
case "checkbox" :
doc.checked = false;
break;
case "radio" :
doc.checked = false;
break;
case "select-one" :
doc.options[doc.selectedIndex].selected = false;
doc.selectedIndex = 0;
break;
case "select-multiple" :
while (doc.selectedIndex != -1)
{
indx = doc.selectedIndex;
doc.options[indx].selected = false;
}
doc.selected = false;
break;
case "textarea":
doc.value = "";
break;
case "file" :
var browser=navigator.appName;
if(browser == 'Microsoft Internet Explorer')
{
var fil = doc;
//fil.select();
n=fil.createTextRange();
n.execCommand('delete');
}
else
{
doc.value='';
}
break;
default :
break;
}
}
}
//function for reset validation controls
function resetmsg(validationControlsPrefix)
{
var spans;
var browser=navigator.appName;
if(browser == 'Microsoft Internet Explorer')
{
spans = document.all.tags('span');
}
else
{
spans = document.getElementsByTagName('span');
}
if (spans)
{
for (var i = 0; i < spans.length; i++)
{
var prefixLength = "" + validationControlsPrefix.length;
var currID = "" + spans[i].id
if ((currID != '') && (prefixLength != ''))
{
if (currID.substring(0,prefixLength) ==
validationControlsPrefix)
{
spans[i].style.display = "none";
}
}
}
}
}
</script>
| You must Sign In to use this message board. | ||||||||
|
||||||||
|
||||||||
|
||||||||
|
||||||||
General
News
Question
Answer
Joke
Rant
Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 9 Nov 2009 Editor: Deeksha Shenoy |
Copyright 2009 by Samrat Banerjee Everything else Copyright © CodeProject, 1999-2010 Web20 | Advertise on the Code Project |