Help...How to Clear textbox in jsp..help

Recommended Answers

All 9 Replies

Use javascript, get the id or the name of the field and set its value to "" empty.

<form name="formName">
<input type="text" name="field" id="field_id" value="aaaaa" />
</form>

Then write code to execute this javascript:

document.formName.field.value="";

//OR

document.getElementById("field_id").value="";

Definitely check this: http://www.w3schools.com/default.asp for more.

i try it but it doesnt work

Post your code. Have you read the tutorials I gave you?

<SCRIPT language="javascript">

function validate(){
    if (document.getElementById('employerName').value=""){
        alert("Fill in Employer Name");
    }
}

function reset(){
    document.getElementById('employerName').value="";
}
function box(){
    alert("Employer Successfully Save");
    reset();
}
</script>

============================================================
<html:form action="/GetCallEmployerActionPath" onsubmit="box();">
    <table style="margin:50px auto; border:1px solid gray;" border="1">
        <tr>
            <td>Employer:</td>
            <td colspan="2">
                <div id="employerName"><html:text property="employerName"/></div>
            </td>
        </tr>
        <tr>
            <td colspan="4" align="right">
                <input type="submit" value="Save"/>
            </td>
        </tr>
    </table>
</html:form>

I am not familiar with these tags:
<html:text property="employerName"/>
So I don't think that this will work:
document.getElementById('employerName').value="";
The employerName in the getElementById must be the id attribute of the text field.

Also if you want any of the javascript functions you have declared to be executed, you need to call them like you did with box function. Using events: onxxxxxx (onclick, onsubmit).

For the onsubmit, the function you call must return true or false.

function validate(){
   if (employee is empty) {
      alert("your error message");
      return false;
   }
   return true;
}

......
<html:form action="/GetCallEmployerActionPath" onsubmit="return validate();">

<html:text property="employerName"/> ====>> this is jsp code using html taglib
if i change it to <input type="text" id="employerName"> it will create an error on my other java action class..
do i need to input the return in my function??

Does this have an id attribute? Can you try to write this:

<html:text property="employerName" id="employerName" />

it doesn't have... :(

Then use something like this and see if it works:

document.<the name of the form the text field is inside>.employerName.value=""
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.