Shadowing Posted May 24, 2012 Share Posted May 24, 2012 Been reading massive examples on how to do this but for some reason I cant get this to work anyone know how to make it where when I hit enter on the keyboard it runs function "post_chat" <form> <button class="post_button" type="button" id="post_button">Send</button> </form> $(function(){ $('button.#post_button').keydown(function(e){ if (e.keyCode == 13) { post_chat(); // function to run after enter is hit return false; } }); }); Quote Link to comment https://forums.phpfreaks.com/topic/263062-trigger-stuff-with-the-enter-key/ Share on other sites More sharing options...
Alex Posted May 24, 2012 Share Posted May 24, 2012 Buttons don't trigger key events like an input would. If you want to listen for the enter button being pressed for the whole page then listen on document.documentElement. $(function(){ $(document.documentElement).keydown(function(e){ if (e.keyCode == 13) { post_chat(); // function to run after enter is hit return false; } }); }); Quote Link to comment https://forums.phpfreaks.com/topic/263062-trigger-stuff-with-the-enter-key/#findComment-1348346 Share on other sites More sharing options...
Shadowing Posted May 24, 2012 Author Share Posted May 24, 2012 thanks on the reply Alex guess im confused also i dont know why im trying to include the button on it. I'm not trying to get enter to hit the button. im trying to get enter to run the function. I tried your example with no luck Quote Link to comment https://forums.phpfreaks.com/topic/263062-trigger-stuff-with-the-enter-key/#findComment-1348349 Share on other sites More sharing options...
Alex Posted May 24, 2012 Share Posted May 24, 2012 Do you have the function post_chat()? I've tried the example and it works. For example try this: $(function(){ $(document.documentElement).keydown(function(e){ if (e.keyCode == 13) { alert('it works!'); return false; } }); }); And press enter on the page. Quote Link to comment https://forums.phpfreaks.com/topic/263062-trigger-stuff-with-the-enter-key/#findComment-1348352 Share on other sites More sharing options...
Shadowing Posted May 24, 2012 Author Share Posted May 24, 2012 hehe i figured out my problem lol omg I forgot my function post_chat is looking for a button pressed lol Thanks alot for the help alex Quote Link to comment https://forums.phpfreaks.com/topic/263062-trigger-stuff-with-the-enter-key/#findComment-1348355 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.