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?
Creates a bulleted unordered list for the selection or at the insertion point. Inserts a paragraph around the selection or the current line. (Internet Explorer inserts a paragraph at the insertion point and deletes the selection.) Inserts the given plain text at the insertion point (deletes selection).
The execCommand() method is deprecated. Do NOT use it. The applets property returns an empty HTMLCollection in all new browsers. The <applet> element is not supported in HTML5.
select() to select the contents of the <textarea> element. Use Document. execCommand('copy') to copy the contents of the <textarea> to the clipboard. Remove the <textarea> element from the document.
I came across this answer but 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 perfectly, not so much for Safari.
document.execCommand("insertHTML", false, "<br>\u200C");
What is \u200c?
Not sure. Found it referenced here.
Dealing with line Breaks on contentEditable DIV
document.execCommand("insertLineBreak");
According to: https://www.w3schools.com/jsref/met_document_execcommand.asp
You can check the specs command at: https://w3c.github.io/editing/docs/execCommand/#dfn-the-insertlinebreak-command
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With