• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1123
  • Last Modified:

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
0
MeenakshiDhar
Asked:
MeenakshiDhar
  • 8
  • 5
  • 4
  • +2
1 Solution
 
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 8
  • 5
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now