0

When my submit Button in the form is clicked, I need to simulate that the "Enter button" is pressed. so: clicking on the submit form is equal to pressing Enter on the keyboard. Because I want to have the same method for both...

Here is what I have, but until now it doesnt works..

$(document).on('click', '#send', function(){
$("#chatMessageTextarea").focus();
var e = jQuery.Event("keypress");
e.which = 13;
e.keyCode = 13;

});

JCoding
  • 109
  • 9

3 Answers3

0

try something like this, not tested it, hope it helps!

function keyUpFunc(e) {
var keyCode_Enter = 13;
    if (e.keyCode === keyCode_Enter) {
        e.preventDefault();
        $('#send').trigger("click");                    
    }
}         

$(document).keyup(keyUpFunc);

$(document).on('click', '#send', function(){
    // Click Event handler for send
});
Kalish
  • 803
  • 1
  • 8
  • 18
  • you can set the keyupfunc for a perticular field as well instead of document if thats what you require. – Kalish Apr 14 '15 at 10:57
0

I am not sure what are you trying to achieve here. But if you want that I think this link has answer to simulate keypress via jQuery.

Definitive way to trigger keypress events with jQuery

Copied code from link:

var e = jQuery.Event("keydown");
e.which = 13; // # Some key code value
$("input").trigger(e);
Community
  • 1
  • 1
sk786
  • 394
  • 1
  • 4
  • 21
0

Try this:

$(document).on('click', '#send', function(){
   $("#chatMessageTextarea").trigger({
        type: "keypress", 
        which: 13, 
        keyCode: 13});
});

hope it helps!

jenjis
  • 1,077
  • 4
  • 18
  • 30