Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert a CR/LF into a textarea

I've got the following code:

document.onkeydown=function(e) {
if (e.which == 13 && isCtrl) {
   log('Ctrl CR');
} else if (e.which == 17) {
   isCtrl = true;
};

I need to insert a Carriage Return/Line feed where the cursor is located in the input textarea. Now that I think about it, I should probably be using a textarea selector instead of document.onkeydown, but $('textarea').onkeydown doesn't work.

like image 456
Phillip Senn Avatar asked Oct 17 '25 15:10

Phillip Senn


1 Answers

$('textarea').keydown(function (e){
    var $this = $(this);
    if (e.which === 13 && e.ctrlKey) {
        $this.val($this.val() + '\r\n'); // untested code (to add CRLF)
    }
});

Reference

  • .keydown
  • Event object
like image 107
Matt Ball Avatar answered Oct 20 '25 04:10

Matt Ball



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!