Click here to Skip to main content
11,485,965 members (76,798 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Javascript
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 = "";
   }
   </script>
<asp:Button ID="clear" runat="server" Text="clear" CausesValidation="false"
                        BackColor="#FF8000" Font-Bold="True" OnClientClick="clear()" />
Posted 13-Nov-12 3:33am
Edited 13-Nov-12 4:58am
v2
Comments
Tadit Dash at 13-Nov-12 9:05am
   
Have you checked that the function is called or not when button is clicked ?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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

Markup:
<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(this.id)" />
</asp:button>

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.

JavaScript:
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.
  Permalink  
v2
Comments
Caleb McElrath at 13-Nov-12 9:34am
   
I should note, I got this from: http://stackoverflow.com/questions/6570352/how-to-clear-textbox-in-javascript-that-contains-value-of-gridview-row-editing
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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 = "";
    }
 
</script>

try this. Smile | :)
  Permalink  

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

  Print Answers RSS
0 C#developer.asp 429
1 Sergey Alexandrovich Kryukov 375
2 Maciej Los 365
3 Mehdi Gholam 250
4 OriginalGriff 144
0 Sergey Alexandrovich Kryukov 8,819
1 OriginalGriff 8,047
2 Sascha Lefèvre 3,199
3 Maciej Los 2,886
4 Richard Deeming 2,370


Advertise | Privacy | Mobile
Web04 | 2.8.150520.1 | Last Updated 13 Nov 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100