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>
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>
Share
Improve this question
asked Feb 19, 2017 at 7:25
nikorionikorio
6914 gold badges17 silver badges28 bronze badges
3
- Possible duplicate of How to make keypress event of asp textbox? – santosh singh Commented Feb 19, 2017 at 7:35
- 1 please add aspx code too – Usman Commented Feb 19, 2017 at 7:38
- 1 what error are you getting in console? – Ram Segev Commented Feb 19, 2017 at 7:38
3 Answers
Reset to default 2your 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./ajax/libs/jquery/3.1.1/jquery.min.js"></script>
try this
<asp:TextBox ID="TextBox1" runat="server" onkeypress="myFunction()"></asp:TextBox>
<script language="javascript">
function myFunction()
{
alert('Key Press')
}
</script>
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.