3

I have got a div with contenteditable that should trigger a < br /> insert on "enter". Everything works fine in IE but Firefox drives me crazy.

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

Firefox ignores < br /> at the end of the div and I guess at the beginning too. Means, if I press "enter" at the middle of a sentence, it works as it should. Trying the same at the end of a sentence (the very last one) it fails.

Any ideas? The same problem we have at the stackoverflow editor preview :-)

Press "enter" at the end > fail ... press "enter" a single letter earlier > newline

Thom
  • 31
  • 1
  • 2
  • 1
    Same problem: http://stackoverflow.com/questions/3080529/make-a-br-instead-of-div-div-by-pressing-enter-on-a-contenteditable – Thom Sep 21 '10 at 21:11
  • insertHTML(\'
    &nbsp\')'; fixes it temporary but cause the ugly nbsp...
    – Thom Sep 21 '10 at 22:43
  • Check out my answer to http://stackoverflow.com/questions/3080529/make-a-br-instead-of-div-div-by-pressing-enter-on-a-contenteditable – Jake McGraw Nov 16 '10 at 23:17

3 Answers3

1

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.

LPL
  • 16,827
  • 6
  • 51
  • 95
Deepak Ranjan Jena
  • 437
  • 5
  • 12
  • 23
0

You can try inserting <br /><span></span>. This moves the cursor to the next line but doesn't properly resize the container so it isn't perfect.

drewrichards
  • 583
  • 4
  • 7
0

contentEditable - Firefox <br /> tag check here, it might give an idea :)

Community
  • 1
  • 1
Shaokan
  • 7,438
  • 15
  • 56
  • 80