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 PluginManse– Manse2012-04-24 09:57:51 +00:00Commented Apr 24, 2012 at 9:57
-
1@ManseUK: Not a duplicate: this question asks for the selected HTML, not the selected text.Tim Down– Tim Down2012-04-24 10:41:53 +00:00Commented Apr 24, 2012 at 10:41
Add a comment
|
1 Answer
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.
2 Comments
Abubakar Shams
this code produce this error Error: Could not convert JavaScript argument arg 0 [nsIDOMHTMLDivElement.appendChild] [Break On This Error] el.appendChild(ranges[i].cloneContents());
Tim Down
@AbubakarShams: Should be fixed now.