2

How can I add a <br/> to <p contenteditable> when the user press the Enter key in jQuery or javascript?

I tried every solution with no happy ending. A div element is always showing.

Currently trying on Chrome browser.

This is the solution for now:

$(this).val($(this).val() + "\n");
setTimeout(function(){
    $($editedDiv).html($('.ui-selected').html().replace(/<div>(.*?)<\/div>/, "<br>$1"));
},100);

The problem with this solution is that tha caret location is moving to the start of the sentence when i press Enter

Dani Banai
  • 1,080
  • 4
  • 14
  • 33
  • 1
    Please edit your question to include the code you have so far. – Jordan Running Mar 02 '17 at 17:12
  • The answer was found here: [http://stackoverflow.com/questions/18552336/prevent-contenteditable-adding-div-on-enter-chrome](http://stackoverflow.com/questions/18552336/prevent-contenteditable-adding-div-on-enter-chrome) Goof luck :) – Dani Banai Mar 12 '17 at 08:04
  • The answer was found here: [click me](http://stackoverflow.com/questions/18552336/prevent-contenteditable-adding-div-on-enter-chrome) – Dani Banai Mar 12 '17 at 08:06
  • Possible duplicate of [Prevent contenteditable adding
    on ENTER - Chrome](http://stackoverflow.com/questions/18552336/prevent-contenteditable-adding-div-on-enter-chrome)
    – Jordan Running Mar 12 '17 at 15:19

1 Answers1

0

This answer may be helpful. Of course, with more details, like which browsers you're having issues in (and maybe some code), we could help you pinpoint the exact problem.

Community
  • 1
  • 1
Rich
  • 3,156
  • 3
  • 19
  • 29
  • The answer was found here: http://stackoverflow.com/questions/18552336/prevent-contenteditable-adding-div-on-enter-chrome – Dani Banai Mar 12 '17 at 08:06