4

I want to press arrow down key automatically when user presses enter on my contenteditable div and here is my code i have tried

$('body').on('keypress', '.chat_txt', function(e) {
       if (e.keyCode === 13) {
            e.preventDefault();
            e.stopPropagation();
            $(this).append('\n'); 
            $(this).trigger({ type: 'keypress', which: 40});
        }
});

But unfortunately this code is fruitless.
JsFiddle https://jsfiddle.net/2q9x4xzm/

Papaa
  • 108
  • 10
  • Possible duplicate of [Simulate Keypress With jQuery](http://stackoverflow.com/questions/1468384/simulate-keypress-with-jquery) – Tibrogargan Nov 05 '16 at 07:51

1 Answers1

1

Make use of the following

var e = $.Event('keypress');
        e.which = 40;
        $('.chat_txt').trigger(e);

Complete code

    $('body').on('keypress', '.chat_txt', function(e) {
console.log(e.keyCode);
       if (e.keyCode === 13) {

            var evt = $.Event('keypress');
            evt.keyCode = 40;
            console.log(evt);
            $('.chat_txt').trigger(evt);
            e.preventDefault();
            e.stopPropagation();
        }
});

})

JSFIDDLE

insertusernamehere
  • 23,204
  • 9
  • 87
  • 126
Shubham Khatri
  • 270,417
  • 55
  • 406
  • 400