Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does execCommand "insertBrOnReturn" work?

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?

like image 251
Matthew Avatar asked Dec 12 '13 19:12

Matthew


People also ask

What does document execCommand do?

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).

Can I use document execCommand?

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.

What is document execCommand (' copy ')?

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.


2 Answers

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

like image 119
christopherdan Avatar answered Sep 28 '22 05:09

christopherdan


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

like image 36
user2280102 Avatar answered Sep 28 '22 03:09

user2280102