feder
feder

Reputation: 1795

Why delete does not work on CKEDITOR when selected?

I'm having a inline editable div. I can type, delete, add. Works great. I wanted the text within he div to be selected on focus (or if you click it). So I added the following to the code

var editor = CKEDITOR.instances.div#{attr};
var element = editor.document.getById('div#{attr}');
editor.getSelection().selectElement( element );

This works too. Fully selected on focus. However, if I press the delete key or any other character key to overwrite the programmatically selected text, it doesn't change. It's as if more than only the text is selected, and the browser doesn't let me delete it. If I select the text manually, it works.

Upvotes: 1

Views: 820

Answers (1)

Reinmar
Reinmar

Reputation: 22023

The selection#selectElement method will start selection before passed element and end after it. This means that not only editable's content will be selected but also non-editable parts of contents outside it and therefore selection may not be editable.

This is a correct solution:

var editor = CKEDITOR.instances.editable,
    el = CKEDITOR.document.getById( 'editable' ),
    range = editor.createRange();

editor.focus();
range.selectNodeContents( el );
range.select();

But the easiest solution is to use selectAll command defined in selectall plugin:

editor.execCommand( 'selectAll' );

Upvotes: 1

Related Questions