How does execCommand "insertBrOnReturn" work?

I tried the following code in Chrome:

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

http://jsfiddle.net/uVcd5/

Wether, I set the last parameter to true or false, the behavior does not change: upon return, new <div> elements will be added.

Must have missed something ... any ideas?

+7
javascript execcommand
source share
2 answers

insertBrOnReturn is specific cmd code for Mozilla, Chrome does not support this. You can check this with:

  document.queryCommandSupported('insertBrOnReturn') 

jsFiddle is here , it warns "true" in Firefox, but "false" in Chrome.

If you only want to insert br, try:

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

Also check this out: Prevent content from being added and lt; div> on ENTER - Chrome

+9
source share

I came across this answer, but I didn’t like how in Chrome, if your cursor is at the beginning of a paragraph, it adds two breaks. Changing the second <br> to \u200C makes Chrome work just fine, not just for Safari.

 document.execCommand("insertHTML", false, "<br>\u200C"); 

What is \u200c? Not sure. Found a link here.

Working with the Gaps line in contentEditable DIV

+2
source share

All Articles