Click here to Skip to main content
15,936,709 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
i used below coding to clear textbox using ., but textbox not getting clear
<script type="text/javascript" language="javascript">
    function clear()
 document.getElementById("<%= txtfname.ClientID %>").value = "";
 document.getElementById("<%= txtlname.ClientID %>").value = "";

<asp:Button ID="clear" runat="server" Text="clear" CausesValidation="false"
                        BackColor="#FF8000" Font-Bold="True" OnClientClick="clear()" />
Updated 13-Nov-12 3:58am
Have you checked that the function is called or not when button is clicked ?

From what I have been seeing the function isn't event called. This is what I got working:

<asp:textbox id="txtfname" runat="server" />
<asp:textbox id="txtlname" runat="server" />
<asp:button id="clear" runat="server" text="clear" causesvalidation="false"
     BackColor="#FF8000" Font-Bold="True"
     OnClientClick="return clearTextBoxes(" />

A few things to note here:
1. I changed the name of the function. This is because if I were to change it to "clear" the function would not be called. I'm guessing due to a naming conflict.
2. I return the return value of the function.
3. I am passing the function the id of the button. This is to obtain the element ids using javascript instead of ASP.

function clearTextBoxes(btnId) {
    var selector = btnId.replace("clear", "txtfname");
    var fname = document.getElementById(selector);

    selector = btnId.replace("clear", "txtlname");
    var lname = document.getElementById(selector);
    fname.value = "";
    lname.value = "";
    return true;

Here I create the id of the first textbox to use in getElementById. Then I get a local variable containing the first textbox. The same thing is done for the second textbox. After the textboxes are obtained, clearing can commence. Finally I return true. Returning "false" instead would keep from a post back. Returning "true" keeps the post back.
Share this answer
Caleb McElrath 13-Nov-12 9:34am    
I should note, I got this from:
Hey buddy your code is working well. actually have given same clear. that is not reserved keyword but its use by DOM. like document.clear.
so better rename it.
second, you are trying to clear button text. but you are submitting form, its mean when its relaod.. it reload its value thou.
These are some modification i have done in your code. its working well check it

<asp:textbox id="txtfname" runat="server" xmlns:asp="#unknown" />
   <asp:textbox id="txtlname" runat="server" xmlns:asp="#unknown" />
<asp:button id="clear" runat="server" text="clear" causesvalidation="false" xmlns:asp="#unknown">
                        BackColor="#FF8000" Font-Bold="True" OnClientClick="clearid()" /></asp:button>

script is here..

<script type="text/javascript">
   // function clearid() {
    //   return false;
   // }
    function clearid() {
        document.getElementById("<%= txtfname.ClientID %>").value = "";
        document.getElementById("<%= txtlname.ClientID %>").value = "";


try this. :-)
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900