Chrome empty contenteditable retaining styling Chrome empty contenteditable retaining styling google-chrome google-chrome

Chrome empty contenteditable retaining styling


One way to prevent Chrome of retaining the old computed styles of the caret position is to clear the browser selection and then restoring it (to retain the selection). This should be done after the deletion of the styled element and before new content gets inserted. Doing something on the line of the following:

let selection = window.getSelection();if (selection && selection.rangeCount > 0 && selection.isCollapsed) {  let range = selection.getRangeAt(0);  selection.removeAllRanges();  selection.addRange(range);}