Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript execCommand('delete') not delete all selection div in chrome

I try to write an editor with contenteditable & execCommand Everything works fine on Firefox, but in chrome has an error with 'delete' command.

Please view bellow photo:

enter image description here

This is my code:

var $obj = $('#myBlockDivId');
var selection = rangy.getSelection();
if (selection.rangeCount > 0) selection.removeAllRanges();
var range = rangy.createRange();
range.selectNode($obj[0]);
selection.addRange(range);
range.select();

when i console log: rangy.getSelection().toHtml() ==> it's right

but when i call:

document.execCommand("delete", null, false);

it's fine on Firefox but not right on Chrome, the wrapper div is not being deleted.

How can i fix this? I have to use execCommand because it's support undo and redo function. so, i can't use jquery or javascript dom selector to remove div.

(I bad at English, someone please edit my question for more clearly, many thanks)

like image 741
Anh Tú Avatar asked Oct 30 '15 05:10

Anh Tú


1 Answers

Maybe you can try to do :
(note the argument alone)

<span contentEditable>
	Select something here and after click on delete.
</span>
<input type="button" value="Delete selection" onclick="document.execCommand('delete')"/>

Acording to w3c.github.io on Enabled commands :

At any given time, a supported command can be either enabled or not. Authors can tell whether a command is currently enabled using queryCommandEnabled(). Commands that are not enabled do nothing, as described in the definitions of the various methods that invoke commands.

Some helpfull links :

w3c Unofficial Draft 15 October 2015:

  • w3c execCommand
  • w3c Deleting the selection
  • w3C The delete command

Other ressources :

  • List of contenteditable Browser Inconsistencies
  • quirksmode.org test page

Hope this will help you !

like image 192
Anonymous0day Avatar answered Sep 29 '22 13:09

Anonymous0day