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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.