Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use `execCommand("insertorderedlist")`?

I see some wysiwyg editor uses execCommand to insert DOM to div, such as: execCommand("insertorderedlist").

I tried to write a demo, but it can't run. The code is simple:

document.execCommand("insertorderedlist");

But when I click "Run JS" button, it doesn't insert anything. Do I miss anything ?

Here is a live demo: http://jsbin.com/olalaf/1/edit

like image 474
Freewind Avatar asked Jul 19 '13 17:07

Freewind


People also ask

What can I use instead of execCommand?

The Clipboard API can be used instead of execCommand in many cases, but execCommand is still sometimes useful.

What is execCommand?

HTML DOM Document execCommand() The applets property returns an empty HTMLCollection in all new browsers. The <applet> element is not supported in HTML5.


1 Answers

Here's your demo fixed by me: http://jsbin.com/olalaf/2/edit

You need an element with contenteditable attribute set to true. This attribute enables editing inside this element. All browsers support it, but in a very poor, buggy way.

Second part is executing document.execCommand in a proper way so the selection isn't lost. That's why I added my own button. I think that Run with JS doesn't work because it's in a different frame, so that the selection is lost before command is executed.

like image 62
Reinmar Avatar answered Sep 20 '22 04:09

Reinmar