Getting selected text with CKEditor Plugin on IE












11














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 doesn't work in IE and I only get [object Object]



Any suggestions?










share|improve this question





























    11














    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 doesn't work in IE and I only get [object Object]



    Any suggestions?










    share|improve this question



























      11












      11








      11


      9





      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 doesn't work in IE and I only get [object Object]



      Any suggestions?










      share|improve this question















      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 doesn't work in IE and I only get [object Object]



      Any suggestions?







      javascript internet-explorer plugins ckeditor






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 23 '17 at 20:19









      Michael

      3,41333756




      3,41333756










      asked Mar 5 '10 at 9:19









      Alex

      58114




      58114
























          5 Answers
          5






          active

          oldest

          votes


















          20














          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();
          }





          share|improve this answer





















          • This just made my day. nice job! and Thank you!
            – AnApprentice
            Mar 8 '10 at 3:08










          • 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
            Mar 12 '10 at 8:23



















          14














          Use:



          editor.getSelection().getSelectedText();


          Or:



          CKEDITOR.instances["txtTexto"].getSelection().getSelectedText()


          "txtTexto" = ID of textarea tag






          share|improve this answer































            3














            To those who want to prefill fields with a selection, just do it like that and safe yourself a long journey.



            onShow: function() {
            this.setValueOf( 'tab-id', 'field-id', editor.getSelection().getSelectedText().toString() );
            },


            Have a nice day!






            share|improve this answer































              2














              @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);





              share|improve this answer































                0














                In the newer versions of CKEDITOR, there seems to be a way easier method:



                var selectedHTML = editor
                .getSelectedHtml()
                .getHtml(); //result: <p>test</p>





                share|improve this answer





















                  Your Answer






                  StackExchange.ifUsing("editor", function () {
                  StackExchange.using("externalEditor", function () {
                  StackExchange.using("snippets", function () {
                  StackExchange.snippets.init();
                  });
                  });
                  }, "code-snippets");

                  StackExchange.ready(function() {
                  var channelOptions = {
                  tags: "".split(" "),
                  id: "1"
                  };
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function() {
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled) {
                  StackExchange.using("snippets", function() {
                  createEditor();
                  });
                  }
                  else {
                  createEditor();
                  }
                  });

                  function createEditor() {
                  StackExchange.prepareEditor({
                  heartbeatType: 'answer',
                  autoActivateHeartbeat: false,
                  convertImagesToLinks: true,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: 10,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader: {
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  },
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  });


                  }
                  });














                  draft saved

                  draft discarded


















                  StackExchange.ready(
                  function () {
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2385609%2fgetting-selected-text-with-ckeditor-plugin-on-ie%23new-answer', 'question_page');
                  }
                  );

                  Post as a guest















                  Required, but never shown

























                  5 Answers
                  5






                  active

                  oldest

                  votes








                  5 Answers
                  5






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  20














                  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();
                  }





                  share|improve this answer





















                  • This just made my day. nice job! and Thank you!
                    – AnApprentice
                    Mar 8 '10 at 3:08










                  • 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
                    Mar 12 '10 at 8:23
















                  20














                  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();
                  }





                  share|improve this answer





















                  • This just made my day. nice job! and Thank you!
                    – AnApprentice
                    Mar 8 '10 at 3:08










                  • 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
                    Mar 12 '10 at 8:23














                  20












                  20








                  20






                  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();
                  }





                  share|improve this answer












                  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();
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 5 '10 at 13:45









                  Luckyrat

                  1,196914




                  1,196914












                  • This just made my day. nice job! and Thank you!
                    – AnApprentice
                    Mar 8 '10 at 3:08










                  • 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
                    Mar 12 '10 at 8:23


















                  • This just made my day. nice job! and Thank you!
                    – AnApprentice
                    Mar 8 '10 at 3:08










                  • 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
                    Mar 12 '10 at 8:23
















                  This just made my day. nice job! and Thank you!
                  – AnApprentice
                  Mar 8 '10 at 3:08




                  This just made my day. nice job! and Thank you!
                  – AnApprentice
                  Mar 8 '10 at 3:08












                  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
                  Mar 12 '10 at 8:23




                  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
                  Mar 12 '10 at 8:23













                  14














                  Use:



                  editor.getSelection().getSelectedText();


                  Or:



                  CKEDITOR.instances["txtTexto"].getSelection().getSelectedText()


                  "txtTexto" = ID of textarea tag






                  share|improve this answer




























                    14














                    Use:



                    editor.getSelection().getSelectedText();


                    Or:



                    CKEDITOR.instances["txtTexto"].getSelection().getSelectedText()


                    "txtTexto" = ID of textarea tag






                    share|improve this answer


























                      14












                      14








                      14






                      Use:



                      editor.getSelection().getSelectedText();


                      Or:



                      CKEDITOR.instances["txtTexto"].getSelection().getSelectedText()


                      "txtTexto" = ID of textarea tag






                      share|improve this answer














                      Use:



                      editor.getSelection().getSelectedText();


                      Or:



                      CKEDITOR.instances["txtTexto"].getSelection().getSelectedText()


                      "txtTexto" = ID of textarea tag







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Feb 24 '12 at 0:38

























                      answered Feb 24 '12 at 0:32









                      Lucas Boemeke

                      15616




                      15616























                          3














                          To those who want to prefill fields with a selection, just do it like that and safe yourself a long journey.



                          onShow: function() {
                          this.setValueOf( 'tab-id', 'field-id', editor.getSelection().getSelectedText().toString() );
                          },


                          Have a nice day!






                          share|improve this answer




























                            3














                            To those who want to prefill fields with a selection, just do it like that and safe yourself a long journey.



                            onShow: function() {
                            this.setValueOf( 'tab-id', 'field-id', editor.getSelection().getSelectedText().toString() );
                            },


                            Have a nice day!






                            share|improve this answer


























                              3












                              3








                              3






                              To those who want to prefill fields with a selection, just do it like that and safe yourself a long journey.



                              onShow: function() {
                              this.setValueOf( 'tab-id', 'field-id', editor.getSelection().getSelectedText().toString() );
                              },


                              Have a nice day!






                              share|improve this answer














                              To those who want to prefill fields with a selection, just do it like that and safe yourself a long journey.



                              onShow: function() {
                              this.setValueOf( 'tab-id', 'field-id', editor.getSelection().getSelectedText().toString() );
                              },


                              Have a nice day!







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited May 24 '17 at 13:57

























                              answered Nov 13 '15 at 16:08









                              Daniel

                              3791521




                              3791521























                                  2














                                  @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);





                                  share|improve this answer




























                                    2














                                    @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);





                                    share|improve this answer


























                                      2












                                      2








                                      2






                                      @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);





                                      share|improve this answer














                                      @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);






                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Nov 23 '11 at 8:49









                                      Hans Olsson

                                      44.2k1278103




                                      44.2k1278103










                                      answered Sep 26 '10 at 6:20









                                      Stephane

                                      211




                                      211























                                          0














                                          In the newer versions of CKEDITOR, there seems to be a way easier method:



                                          var selectedHTML = editor
                                          .getSelectedHtml()
                                          .getHtml(); //result: <p>test</p>





                                          share|improve this answer


























                                            0














                                            In the newer versions of CKEDITOR, there seems to be a way easier method:



                                            var selectedHTML = editor
                                            .getSelectedHtml()
                                            .getHtml(); //result: <p>test</p>





                                            share|improve this answer
























                                              0












                                              0








                                              0






                                              In the newer versions of CKEDITOR, there seems to be a way easier method:



                                              var selectedHTML = editor
                                              .getSelectedHtml()
                                              .getHtml(); //result: <p>test</p>





                                              share|improve this answer












                                              In the newer versions of CKEDITOR, there seems to be a way easier method:



                                              var selectedHTML = editor
                                              .getSelectedHtml()
                                              .getHtml(); //result: <p>test</p>






                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Nov 16 '16 at 5:40









                                              K48

                                              5,36993990




                                              5,36993990






























                                                  draft saved

                                                  draft discarded




















































                                                  Thanks for contributing an answer to Stack Overflow!


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid



                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.


                                                  To learn more, see our tips on writing great answers.





                                                  Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                  Please pay close attention to the following guidance:


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid



                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.


                                                  To learn more, see our tips on writing great answers.




                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function () {
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2385609%2fgetting-selected-text-with-ckeditor-plugin-on-ie%23new-answer', 'question_page');
                                                  }
                                                  );

                                                  Post as a guest















                                                  Required, but never shown





















































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown

































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown







                                                  Popular posts from this blog

                                                  What visual should I use to simply compare current year value vs last year in Power BI desktop

                                                  Alexandru Averescu

                                                  Trompette piccolo