Abubakar Shams
Abubakar Shams

Reputation: 149

How to get selected html from CKEDITOR in javascript?

I am having problem to get selected html written in ckeditor in javascript in my drupal project. can any one help me out?

Upvotes: 2

Views: 3534

Answers (1)

Tim Down
Tim Down

Reputation: 324567

CKEditor's API doesn't provide exactly this, but looking at the docs it looks as though you could do the following (untested):

function getSelectionHtml(editor) {
    var sel = editor.getSelection();
    var ranges = sel.getRanges();
    var el = new CKEDITOR.dom.element("div");
    for (var i = 0, len = ranges.length; i < len; ++i) {
        el.append(ranges[i].cloneContents());
    }
    return el.getHtml();
}

alert( getSelectionHtml(editor) );

CKEditor also has HTML parsing and serialization APIs that I don't know much about, so you may be able to use those to tailor the HTML to your requirements rather than use the raw output from the browser's innerHTML implementation.

Upvotes: 5

Related Questions