3

I'm trying to get KeyPress Event for TextBox in ASP.NET, so I found this script, but it does not works for me

<title></title>

    <script src="jquery.js" type="text/javascript"></script>
    <script type="text/javascript"  lang="js">
        $(function () {
            $("#<%=TextBox1.ClientID %>").keypress(function () {
                alert("Wow; Its Work!.")
            });
        });
    </script> 

</head>
3
  • Possible duplicate of How to make keypress event of asp textbox? Commented Feb 19, 2017 at 7:35
  • 1
    please add aspx code too Commented Feb 19, 2017 at 7:38
  • 1
    what error are you getting in console? Commented Feb 19, 2017 at 7:38

3 Answers 3

2

your script is working very well the problem might be in jquery file its not found in the given location you can change the src for check

<script src="jquery.js" type="text/javascript"></script>

To

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
Sign up to request clarification or add additional context in comments.

Comments

2

try this

<asp:TextBox ID="TextBox1" runat="server" onkeypress="myFunction()"></asp:TextBox>

 <script language="javascript">
    function myFunction()
    {
        alert('Key Press')
    }
</script>

Comments

1

Like Mostafa wrote, it is important to insert the script at the END of the page. So that the page can register the controls first.

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.