Jump to content

Trigger stuff with the enter key


Shadowing

Recommended Posts

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;
        }
    });
});

Link to comment
https://forums.phpfreaks.com/topic/263062-trigger-stuff-with-the-enter-key/
Share on other sites

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;
        }
    });
});

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.