Javascript submit textbox on ENTER

harisdev picture harisdev · Jan 17, 2012 · Viewed 76.9k times · Source

Possible Duplicate:
Bind textbox to 'enter' key

I've made ajax / jquery chat and there is no form in textbox for message , so i can't submit that message with ENTER on keyboard.Is there any way to submit that value from textbox with javascript ?

Thanks.

Answer

user625860 picture user625860 · Jan 17, 2012
document.getElementById("id_of_your_textbox").addEventListener("keydown", function(e) {
    if (!e) { var e = window.event; }
    e.preventDefault(); // sometimes useful

    // Enter is pressed
    if (e.keyCode == 13) { submitFunction(); }
}, false);