Reputation: 149
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
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