Tuesday, 10 September 2013

get selected html in ckeditor

get selected html in ckeditor

I'm working on a custom generator of link in CKEditor. What I would like
to do is getting the html that has been selectioned by the user. I tried a
lot of thing, my last function is that
function getTheHtml (editor) {
var selection = editor.getSelection();
if (selection.getType() == CKEDITOR.SELECTION_ELEMENT) {
var selectedContent = selection.getSelectedElement().$.outerHTML;
} else if (selection.getType() == CKEDITOR.SELECTION_TEXT) {
if (CKEDITOR.env.ie) {
selection.unlock(true);
selectedContent = selection.getNative().createRange().text;
} else {
selectedContent = selection.getNative();
}
}
return selectedContent;
}
this works pretty well but I still have issue, if the user select text it
works, if he select an image it works but when the user select an image
plus some text i only get the text for exemple this is what the user
really select
<img align="" alt="" border="0" class="donotlink" hspace="5"
src="image/pic.jpg" vspace="5" />Some Random Text
and this is what i get from my function
Some Random Text
I know this has been a frequent topic but since I didn't found how to
fix... Thanks in advance.

No comments:

Post a Comment