user2944673
user2944673

Reputation: 91

resetting the textbox through client side using javascript in asp.net

I want to reset the text box through client side using javascript. I used following ways to reset the textbox. But, they are not resetting.

cText = "";
document.getElementById("<%=txtText.ClientID %>").value = cText;

and

document.getElementById("<%=txtText.ClientID %>").value = "";

Please can anyone help me in this?

code:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script type="text/javascript">
        function validate() {
            var cText = document.getElementById('<%=txtText.ClientID %>').value;

            if (cText != "") {
                alert("Text is not null.");
                document.forms['UserMaster'].elements['txtText'].focus();
                $("#<%=txtText.ClientID %>").val("");
                return false;
            }

            return true;
        };
    </script>
</head>
<body>
    <form id="form" runat="server">
    <div class="page">
                <asp:TextBox ID="txtText" runat="server"></asp:TextBox>
                <br/>
                <asp:Button ID="btnSubmit" runat="server" Text="Submit" Font-Bold="true" OnClientClick="return validate()" 
                ForeColor="#9370DB" Height="23px" Width="65px" OnClick="btnSubmit_Click"
                />
            <br />
   </div>
     </form>
</body>
</html>

Upvotes: 0

Views: 746

Answers (2)

Shruti
Shruti

Reputation: 1

I had the same problem and using this helped me solve the issue. Here you can inspect the control from the browser and get the ID of that control.

// ControkClientID is the ID from Inspect element
document.getElementById(ControlClientID).value='';

Upvotes: 0

Krunal Patil
Krunal Patil

Reputation: 3676

Here is the solution for your issues:

document.getElementById('<%=txtText.ClientID %>').value='';

you were using double quotes which was not working for you, whereas you need to use single quotes as show in the above code. This was your issues.

.value=" ";   //is Wrong

Happy Coding.

Upvotes: 1

Related Questions