Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using inline CKEditor on dynamically added text

Tags:

ckeditor

I am building a web app that uses javascript to dynamically add elements to the page, that can then be edited using contentEditable="true" and CKEditor.

Currently if I add an element to the page with contentEditable="true", the element is editable but the CKEditor toolbar is not appearing.

I have tried calling CKEDITOR.inlineAll() but this doesn't seem to do anything.

How can I activate CKEditor inline editing on dynamically created elements? (Without refreshing the page).

EDIT: I have found that giving the element an ID of (e.g.) someId and calling CKEDITOR.inline(someId) has the desired effect. But I don't want to have to add unique IDs to all of my elements. Is there a way to activate CKEditor on all contentEditable elements?

like image 863
Jon Avatar asked Mar 22 '23 13:03

Jon


1 Answers

CKEDITOR.inline accepts a native DOM element as a parameter. No matter how you create dynamic elements, if you pass a reference to that function, it will convert it into CKEditor instance. For example, assuming that you use jQuery as a main framework:

// A dynamically created element.
var el = $( '<p contenteditable="true">I\'m editable!</p>' );

// Append the element to <body>.
$( 'body' ).append( el );

// CKEDITOR.inline accepts DOM element as parameter.
CKEDITOR.inline( el.get( 0 ) ); 

See the fiddle.

like image 85
oleq Avatar answered Apr 02 '23 05:04

oleq