Javascript execCommand ('delete') doesn't delete all div selection in chrome

I am trying to write an editor with contenteditable and execCommand. Everything works fine in Firefox, but in chrome there is an error with the "delete" command.

Please view the following 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 right

but when i call:

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

this is good for Firefox, but not correct in Chrome, the wrapper div is not removed.

How can i fix this? I have to use execCommand because it supports undo and redo function. so i cant use jquery or javascript dom selector to remove div.

(I'm bad in English, someone, please edit my question for a clearer, thank you very much)

+5
source share
2 answers

Perhaps you can try:
(pay attention only to the argument)

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

According to w3c.imtqy.com on Included Commands :

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

Some useful links:

w3c Informal project October 15, 2015 :

Other resources :

Hope this helps you!

+3
source

Try:

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

Instead:

 document.execCommand("delete", null, false); 
+1
source

Source: https://habr.com/ru/post/1234840/


All Articles