Div contenteditable and hit enter on FF

I have a div with contenteditable that should run paste on “enter.” Everything works fine in IE, but Firefox is driving me crazy.

this.e.keydown(function(event) {
  if(event.keyCode == 13) {
    var execute = editor.insertHTML(\'<br />\')';
    eval(execute);
    return false;
  } 
});

Firefox ignores the end of the div, and I think the beginning too. So if I press enter in the middle of the sentence, it works as it should. Trying to do the same at the end of the sentence (the very last) he fails.

Any ideas? The same problem as in the preview editor stackoverflow : -)

Press "enter" at the end> ​​crash ... press "enter" one letter earlier> newline

+3
source share
3 answers

Add this function instead

this.e.keydown(function(event) {
    if (event.keyCode == 13 || event.charCode == 13) {
        var execute = editor.insertHTML(\'<br />\')';
        eval(execute);
        return false;
    } 
});

FF reads charCode instead of keyCode.

+1
source

You can try pasting <br /><span></span>. This moves the cursor to the next line, but does not resize the container so that it is not perfect.

0
source

contentEditable - Firefox tag here, it can give an idea :)

0
source

Source: https://habr.com/ru/post/1765885/


All Articles