Thom
Thom

Reputation: 31

Div contenteditable and pressing "enter" on FF

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

Upvotes: 3

Views: 3436

Answers (3)

Deepak Ranjan Jena
Deepak Ranjan Jena

Reputation: 437

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.

Upvotes: 1

Shaokan
Shaokan

Reputation: 7684

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

Upvotes: 0

drewrichards
drewrichards

Reputation: 583

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.

Upvotes: 0

Related Questions