0

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>
9
  • 5
    Typo: getElemenyById should be getElementById. Commented May 21, 2014 at 7:26
  • @RGraham, thank you for correction,i didn't see that. Commented May 21, 2014 at 7:30
  • I tried with $("#txtText").val("");. But it did not work. Commented May 21, 2014 at 7:32
  • how about this one : document.getElementById('<%=txtText.ClientID %>').innerText=''; Commented May 21, 2014 at 7:38
  • how about $("#<%=txtText.ClientID %>").val(""); Commented May 21, 2014 at 7:48

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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='';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.