views:

493

answers:

2

I have made a plugin for CKEditor, but it relies on the currently selected text.

In FF and Chrome I can use:

var selectedText = editor.getSelection().getNative();  

but this doesnt work in IE and I only get [object Object]

Any suggestions?

+2  A: 

This is what I use:

var mySelection = editor.getSelection();

if (CKEDITOR.env.ie) {
    mySelection.unlock(true);
    selectedText = mySelection.getNative().createRange().text;
} else {
    selectedText = mySelection.getNative();
}
Luckyrat
This just made my day. nice job! and Thank you!
AnApprentice
OK, that looks good, but where do I put that in my dialog code? I thought it would be under onShow:function( HERE ) but I get the error that selectedText is undefined when I use the code below in the contents section. type:'html', html:selectedText
Alex
A: 

@TheApprentice

You put it like this:

( function(){

var getSelectedText = function(editor) { var selectedText = ''; var selection = editor.getSelection(); if (selection.getType() == CKEDITOR.SELECTION_TEXT) { if (CKEDITOR.env.ie) { selection.unlock(true); selectedText = selection.getNative().createRange().text; } else { selectedText = selection.getNative(); } } return(selectedText); }

...

with a call like this:

onShow: function() {
  // Get the element currently selected by the user
  var editor = this.getParentEditor();
  var selectedContent = getSelectedText(editor);
Stephane