2

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

2
  • possible duplicate of Getting selected text win CKEditor Plugin Commented Apr 24, 2012 at 9:57
  • 1
    @ManseUK: Not a duplicate: this question asks for the selected HTML, not the selected text. Commented Apr 24, 2012 at 10:41

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

this code produce this error Error: Could not convert JavaScript argument arg 0 [nsIDOMHTMLDivElement.appendChild] [Break On This Error] el.appendChild(ranges[i].cloneContents());
@AbubakarShams: Should be fixed now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.