0

I'm trying to append a br tag after a user click enter in a textarea field.

This is what I'm trying to do:

if (e.keyCode == 13)
{
   $('.myTextarea').replace('\n', "<br />");       
}

Can anyone tell me what I'm doing wrong?

Rodrigo Werlang
  • 2,118
  • 1
  • 17
  • 28
Pat
  • 117
  • 1
  • 9
  • 2
    Possible duplicate of [New Line in Textarea to be converted to
    ](https://stackoverflow.com/questions/5999792/new-line-in-textarea-to-be-converted-to-br)
    – Zak Apr 17 '18 at 22:21

1 Answers1

0

You should get value in the textarea. See the val document http://api.jquery.com/val/.

Try this,

if (e.keyCode == 13)
{
    $('.myTextarea').val(function(i, v) {
        return v.replace('\n', '<br/>');
    }
}
oh3vci
  • 72
  • 1
  • 7