1

I have a search form with a myName textfield.

On load, the field in the form is set using session attribute.

Something like:

<% String  nameValue = (String)session.getAttribute("nameValue"); %>

And below code where the text field is defined:

<form name="myForm" method="POST">
   <input type="text" name="myName" id="myName" size="45" maxlength="49" value="<%=nameValue%>">
</form>

In the same form I have a reset field defined as below:

<input type="reset" value="Reset" id="Reset"/>

However, on clicking this field, the myName field is not getting empty.

I also tried defining a javascript function:

<input type="reset" value="Reset" onclick="fnFormReset()"/>

function being:

function fnFormReset(){
            alert("here");
            document.myForm.myName.value = "";
        }

However, this is also not working.

Any solution ??

Vicky
  • 16,679
  • 54
  • 139
  • 232
  • check these link http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files – niko Nov 09 '11 at 13:30
  • can you please explain your answer. Am not getting it ?? does it mean all the fields (radio buttons, dropdowns, etc.) will be reset to blank values ??? – Vicky Nov 09 '11 at 13:39

1 Answers1

3

The <input type="reset"> does not clear the input values. It just resets the input values to its initial state. Change the value and then click the reset button. Its actual job will be clear.

As to the problem with the JS function; the onclick will run before the type="reset" does its default job (redisplaying the original value). So, you need to either change type="reset" to type="button":

<input type="button" value="Reset" onclick="fnFormReset()"/>

or to add return false; to end of onclick to block the default action:

<input type="reset" value="Reset" onclick="fnFormReset(); return false;"/>

Unrelated to the concrete problem, I suggest to use EL (Expression Language) to redisplay the value:

value="${nameValue}"

this way you don't need that ugly <% %> anymore to get the session attribute. Also, whenever it concerns user-controlled input, then EL offers you the possiblity to prevent XSS attacks by escaping it using JSTL fn:escapeXml():

value="${fn:escapeXml(nameValue)}"

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks!! that worked.. a doubt though: what is the difference between adding "return false;" in input tag onclick attribute and adding return false; as the last line of fnFormReset() ?? because the latter did not work... – Vicky Nov 09 '11 at 13:36
  • Because you're ignoring the function's return value. Use `onclick="return fnFormReset();"`. But for your particular requirement, using `type="button"` is really better, because you actually don't want to **reset** (reinitialize default values) the form at all, but instead you want to **clear** (remove all values) the form. – BalusC Nov 09 '11 at 13:40