How does execCommand "insertBrOnReturn" work?

Matthew picture Matthew · Dec 12, 2013 · Viewed 7.7k times · Source

I tried the following code on Chrome:

document.execCommand("insertBrOnReturn", false, true);

http://jsfiddle.net/uVcd5/

Wether I set the last parameter to true or false, the behaviour doesn't change: on return, new <div> elements will be added.

Must have missed something... any ideas?

Answer

Andrew picture Andrew · Dec 16, 2013

insertBrOnReturn is a Mozilla specific command, Chrome doesn't support it. You can test that with:

 document.queryCommandSupported('insertBrOnReturn')

jsFiddle here, it alert true in Firefox, but false in Chrome.

If you want to insert <br> only, try:

document.execCommand('insertHTML', false, '<br><br>');

Also, check this: Prevent contenteditable adding <div> on ENTER - Chrome