javascript - Ajax call not running onKeydown -


i have ajax call that's suppose run when key pressed on specific textbox. once in call, runs function fires alert. but, doesn't work. maybe it's cause wrote call using old forum post reference. assume, called jquery library cause on test did didn't post here.

this i've tried far:

<script>        $("#<% =tb.clientid%>").keydown         (             function ()             {                 debugger; alert("hello");             }         ); </script>  <body>     <asp:textbox id = "tb" runat = "server"/>  </body> 

i'm new these kind of calls. i'm familiar js functions, i've never done this. explanations , suggestions, appreciated.

html processed line line. when processes contents of script tag, hasn't yet processed further down, such body tag or contents.

inside script, you're running $("#<% =tb.clientid%>"). it'll try find element id, since body hasn't been processed yet, yield no results. no results, has nothing on set listener .keydown.

it's in cases these using jquery's $(document).ready function or equivalents becomes incredibly important , crucial code. $(document).ready accepts function, , wait until dom loaded before executing code. putting $("#<% =tb.clientid%>").keydown.... inside of $(document).ready ensure element has chance enter dom before .keydown listener attached it.

you can find docs $(document).ready() here.


Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -