document.forms[0].reset() not working after submitting the page

I have one asp.net page (Page1) with llots of controls.
After submitting this page, I have another page (Page2) on which there is one link which redirects me again to the same page from which I came from but with prefilled values. On the original page (Page1), there is one link "Clear Fields". Onclick of this link I want to clear all the controls.
   document.forms[0].reset() --- Not working in this case.

Before submitting the form, if I fill up the values and click on "Clear Fields" link, I am able to reset all the controls but once I submit the form and came back to the same page, the above javascript code is not working.

Can anyone help me!!

Thanks in advance,
Meenakshi Dhar
MeenakshiDharAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David S.Commented:
reset() sets fields back to the way they were when the page loaded. So if the text fields had values, then those values would used. Otherwise, blank values would be used.

So if you want to clear values form text fields that had values when the page loaded, then you'll need to write a function to iterate over the fields and give each a blank value.

This should do it:  (I wrote it from memory and didn't test it.)
// call it by:  clearFields(document.forms[0])
 
function clearFields(form) {
  if(!form || !form.elements) return;
  var re = /text|password/i;
  for(var i=0,k=form.elements.length;i<k;i++){
    if(form.elements[i].type && re.test(form.elements[i].type)) {
      form.elements[i].value='';
    }
  }
}

Open in new window

0
MeenakshiDharAuthor Commented:
Thanks Kravimir for the solution..
I am having different controls on my page like dropdownlist & checkboxes etc...Need to clear those as well..??
And one more thing, which I am concerned about now is the performance. Since I am having lots of controls (dynamically created at run time), so just want to know how much willl be the performance difference between these two:
     document.forms[0].reset()
                    OR
      clearFields(document.forms[0])
0
David S.Commented:
You're welcome.  Here's a function that  can handle more form control types.  I used a switch in this function, because it should run a little faster than running RegExp.test() 3 times per iteration.

I suppose reset() would have better performance, but there's a decent chance that you wouldn't be able to tell the difference.
function clearFields(form) {
  if(!form || !form.elements) return;
  for(var i=0,k=form.elements.length;i<k;i++){
    if(typeof(form.elements[i].type)!='string') continue;
    switch(form.elements[i].type.toLowerCase()) {
      case 'text':
      case 'textarea':
      case 'password':
        form.elements[i].value='';
        break;
      case 'radio':
      case 'checkbox':
        form.elements[i].checked=false;
        break;
      case 'select-one':
      case 'select-multiple':
        form.elements[i].selectedIndex=-1;
        break;
    }
  }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MeenakshiDharAuthor Commented:
Thanks Kravimir once again. Your solution is perfectly fine.
But in my case, the requirement is little bit different. I forgot to mention that earlier.

Some controls on the page should maintain their state...
When you click on Page1, some checkboxes are already checked & dropdownlist are having some  prefilled selections.
Onclick of "Clear Fields" link, these settings should not be cleared.

Do you want me to create another ticket for this? Or can we continue with the same?
0
David S.Commented:
You're welcome. Yes, I would appreciate it if you accepted my last reply as the answer and asked a new question.  Be sure to provide more details about which fields should not be cleared.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.