• Post Reply Bookmark Topic Watch Topic
  • New Topic

Javascript reset() method and JSF h:form  RSS feed

 
Gina vernon
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I created a JSP with a JSF h:form with login elements such as text input field and a password field. I included some javascript in the JSP header such that each time the html body loads or unloads, the h:form is reset. The results are not consistent. I tested using tomcat's internal web browser, safari and IE on Mac.
When I first start tomcat and load the jsp page, the reset works fine. Going back and forth between pages resets the fields. When the I reload the page within the same server connection, the text field remains populated and is not reset. This is true even when I click the reset button. The reset button redundancy is just for practice.

In the javascript function, I used the h:form's ID value since it doesn't have a name attribute.


Here is my JSP code:



What am I missing?

Thanks.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66144
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please be sure to ask JSF questions in the JSF forum. I have moved this post there for you.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure why Bear moved it since it is a JavaScript problem

document.forms.loginfo.reset() is not valid

It is either

document.formName.reset();
or
document.forms["formName"].reset();
or
document.getElementById("formId").reset();

Eric
 
Gina vernon
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eric Pascarello wrote:I am not sure why Bear moved it since it is a JavaScript problem


Thats what I thought.


Anyway, I gave all your recommendations a try, the problem persists. One other thing I have noticed is that the second time I load the page within the same server connection, the reset button doesn't work either. It looks like the value from the previous page load is considered the default.
Is there any server cache setting I can change to fix it?

Thanks.
Gina
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can create a function and set the values with anything you want, but that would require you to reference each element of the form.

Eric
 
Tim Holloway
Bartender
Posts: 18662
71
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It think the problem is in understanding what Reset does.

Reset reverts all form values to whatever they were when the page was loaded. In JSF, however, if you move off the page using a commandbutton or commandlink, the normal mode of operation is to update the backing bean from the form. Then if you go back to that form, the page is reloaded with those updated values. Reset cannot revert to the older original values since they're not what was in the form when it was loaded.

A more powerful type of reset that CAN handle things like that can be done by adding a commandbutton labelled "Reset" that has 'immediate="true"' set and attaches to an action method on the backing bean that resets the bean's fields to their original values. Since the command action also redisplays the page (assuming you haven't set up to navigate somewhere else, instead), the page will be reloaded with those updated backing bean values. And the HTML/JavaScript reset runction would then revert to those values.

And, yes this does mean that you could totally confuse the user by placing two different reset buttons on the page; one that talks to the server and resets from the backend and the other being a traditional HTML form reset that only resets the current inputs.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!