editor.delete()
Removes whatever is currently selected in the editor. With a range of text selected it deletes that text; with a control such as an image selected it removes the image. It is the programmatic equivalent of the user selecting content and pressing Delete.
Syntax
editor.delete()
Parameters
None.
Returns
Nothing (undefined).
Behavior notes
- A selected control takes priority. If a control (an image, for instance) is selected,
delete()removes that node and places the caret where it was, regardless of any text selection state. - A collapsed caret deletes nothing. When the selection is just a caret with no highlighted text,
delete()is a no-op — it does not forward-delete the next character. Select a range first if you need content removed. - A text range is removed in place, collapsing the surrounding content together as expected.
Example usage
Select the whole document and clear it:
var editor1 = new RichTextEditor("#div_editor1");
editor1.setHTMLCode("<b>hello world</b>");
editor1.selectDoc();
editor1.delete();Delete a specific control — select it first, then delete. Here the selected image is removed:
var img = editor1.getEditable().querySelector("img");
if (img) {
editor1.selectControl(img);
editor1.delete();
}Related
- editor.selectControl(element)— select an image or other control before deleting it.
- editor.selectDoc()— select the entire document.
- editor.getSelectedControl()— check whether a control is currently selected.