document.forms[0].reset not working

With reference to this -
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_24440520.html#a24481004


I am having one asp.net page (Page1) with lots of controls (textbox, dropdownlist, radiobutton, checkboxes). Some of the controls are already prefilled like some radiobuttons are checked, dropdownlist already prefilled with some values. I eneterd some values in the controls.
On this page (Page1), there is one link "Clear Fields". Onclick of this link I reset all the controls.
      document.forms[0].reset() -- using this I can reset the values successsfully

After this I submitted Page1, I have another page (Page2) on which there is one link which redirects me again to the same page (Page1) alongwith the values which I entered before submitting.
Now if I click on "Clear Fields" link, I am not able to reset values using "document.forms[0].reset()"..

What I want is -
I shouls be able to reset the values even after coming back to the Page1. But default values should be there (same as when you visit Page1 first time)

Can anyone help me!!

Thanks in advance,
Meenakshi Dhar
MeenakshiDharAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

HainKurtSr. System AnalystCommented:
put a button, write

server.transfer("samePage.aspx", false)

to the click event... this will load the page from the beginning...
0
MeenakshiDharAuthor Commented:
Is there anything we can do using link instead of button. Cannot change the requirement..
0
HainKurtSr. System AnalystCommented:
link? mean

<a href="SamePage.aspx">Clear Fields</a>

or if it is a command link, then write transfer code to the event handler...
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

HainKurtSr. System AnalystCommented:
or write a js, and call this from the link, more safe in your case

function ResetForm() {
  document.all.input1.value = "";
  document.all.input2.value = "1"; // these are reset values, the initial values
  document.all.input3.value = "CA";
  document.all.input4.value = "";
...
}
0
MeenakshiDharAuthor Commented:
I think there is some confusion..

Page1:
some controls, submit button and "Clear Fields" link...
onclick of this link - document.forms[0].reset; -- working fine before submitting

after submitting Page1, gets into Page2.
Page2:
one link "Back to page1", this redirects me to Page1 with the values I entered before submitting..
Now onclick of "Clear Fields" link - document.forms[0].reset; -- not working


0
HainKurtSr. System AnalystCommented:
actually it works, before clicking it change some values again and click it, you will see it resets the value to the ones when page loads, not initial values you want... therefore;

instead of using document.forms[0].reset;, use the a js function similar to above...

onClick="javascript:ResetForm();"

it works every time...
0
MeenakshiDharAuthor Commented:
you r rite but in my case the controls are dynamically created on run time..
I can use something like below but the only prob I want the default values back (when page1 loads first time, some controls are already having some values).
Using below function will make checkboxes clear & dropdownlist showing empty selection.
 
// call it by:  clearFields(document.forms[0])
 
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;
    }
  }
}
0
HainKurtSr. System AnalystCommented:
whats the problem with this solution?

<a href="SamePage.aspx">Clear Fields</a>

it opens the firs page again, with all the values populated by defaults... this one should work too...
and it is more clean and simple...
0
MeenakshiDharAuthor Commented:
With above solution, it will reload the page again (cannot do as per requirement).
0
MeenakshiDharAuthor Commented:
Can anyone please help..
0
ZylochCommented:
What exactly is the code behind the link on Page 2 that goes back to Page 1? That is, how does it redirect back to the previous page with all the form fields filled with the same values? In the browser View Source (not your source code, but actually from the browser), what is the new Page 1 code (after going back from Page 2)?
0
MeenakshiDharAuthor Commented:
Page1.aspx - URL of Page1.aspx -- > http://mydomain.com/Page1.aspx

Filled some values and click on "Search" button --> Page2.aspx

URL of Page2.aspx:

http://mydomain.com/Page2.aspx?mdatemaxage=43200&rb=1&tjti=1&tjtpt=1&tsni=1&clv=12+13&tcc=&q=sales&rlid=336&actionMethod=2

Link on Page2.aspx which redirects back to Page1.aspx is "Back Link"
URL of "Back Link" -->

http://mydomain.com/Page1.aspx?mdatemaxage=43200&rb=1&tjti=1&tjtpt=1&tsni=1&clv=12+13&tcc=&q=sales&rlid=336&actionMethod=2

0
ZylochCommented:
So in this case Page 1 is completely reloaded again? Can I see the HTML code that you get from going to View Source in the browser after clicking on the link in Page 2 to go back to Page 1? Maybe something in the source of Page 1 after that is different from how it was before and is causing the discrepancy.
0
MeenakshiDharAuthor Commented:
The page source is quite big.. I have compared both the files (Page1 & Page2) but did not find any difference except controls filled with values in Page 2.
0
ZylochCommented:
I thought it was when you returned to Page 1 that the Clear Fields link on Page 1 didn't work. Did the source change anywhere at all between the two instances of Page 1 besides the forms being filled in? If not, can you post the source of the Clear Fields link? Also, you might want to try giving your form a name, and calling document.forms['form_name'].reset().
0
MeenakshiDharAuthor Commented:
"Clear Fields link" simply calls this :

<a href="javascript:ClearAll()">

function ClearAll()
{
       document.forms[0].reset();
}

0
ZylochCommented:
Did you try giving your form a name attribute and using

    document.forms['FORM_NAME'].reset();
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
Obadiah ChristopherCommented:
Well, the Server.Transfer should work, If you use a linkButton control. You need not put a button control.
0
CWS (haripriya)Commented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
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.