Get selected text and rewrite it

Last post 03-20-2009, 1:32 AM by Kenneth. 9 replies.
Sort Posts: Previous Next
  •  11-25-2005, 3:06 PM 13028

    Get selected text and rewrite it

    I have the following Editor declaration:
          <ce:editor ID="Editor" Runat="server"
            AutoConfigure="Full"
            AutoParseClasses="true"
            BorderWidth="0"
            DisableAutoFormatting="false"
            EnableStripScriptTags="false"
            FilesPath="/sitecore/Shell/Editor"
            Focus="false"
            FrameStyle="margin:0"
            Height="100%"
            ShowDecreaseButton="false"
            ShowEnlargeButton="false"
            ThemeType="Office2003"
            Width="100%"
            UseRelativeLinks="true"
            UseSimpleAmpersand="true">
          </ce:editor>
    When I try to get a selected text via JScript code:

      var editor1 = document.getElementById('CE_Editor_ID'); 
      var editselection=editor1.GetSelection();  
      if(editselection.rangeCount > 0)
      {
                    var r=sel.getRangeAt(0);
                    var clonedSelection = r.cloneContents();
                    var div = document.createElement('div');
                    div.appendChild(clonedSelection);
                    r.collapse(true);
                    alert(div.innerHTML);
       }

     
    I can not get property editselection.rangeCount worked. It looks like this property does not exist. Where I was wrong?
    How can I get a selected HTML text and replace it with my own?
    I used the example from the forum but it seems does not work: http://www.cutesoft.net/forums/10645/ShowPost.aspx 
    As I can see from assembly I get 5.0 version. Thank you in advance.  
  •  11-25-2005, 4:08 PM 13032 in reply to 13028

    Re: Get selected text and rewrite it

    >>I can not get property editselection.rangeCount worked. It looks like this property does not exist. Where I was wrong?
    The rangeCount only works in Gecko based browsers, like firefox, netscape...
     
     

    asp.net Chat http://cutesoft.net/ASP.NET+Chat/default.aspx
    Web Messenger: http://cutesoft.net/Web-Messenger/default.aspx
    asp.net wysiwyg editor: http://cutesoft.net/ASP.NET+WYSIWYG+Editor/default.aspx
    asp wysiwyg html editor: http://cutesoft.net/ASP
    asp.net Image Gallery: http://cutesoft.net/ASP.NET+Image+Gallery/default.aspx
    Live Support: http://cutesoft.net/live-support/default.aspx

  •  11-25-2005, 4:12 PM 13033 in reply to 13032

    Re: Get selected text and rewrite it

    >>How can I get a selected HTML text and replace it with my own?

        var editor1 = document.getElementById('<%=Editor1.ClientID%>');
        var editselection=editor1.GetSelection();  
        var r= editselection.createRange();

        alert(r.htmlText); // get a selected HTML text

    You should use the following javaScript API instead of writing your own:

    function PasteHTML(html)
    {
        // get the cute editor instance
        var editor1 = document.getElementById('<%=Editor1.ClientID%>');
        
        editor1.PasteHTML(html);
    }
     
    Keep me posted
     
     

    asp.net Chat http://cutesoft.net/ASP.NET+Chat/default.aspx
    Web Messenger: http://cutesoft.net/Web-Messenger/default.aspx
    asp.net wysiwyg editor: http://cutesoft.net/ASP.NET+WYSIWYG+Editor/default.aspx
    asp wysiwyg html editor: http://cutesoft.net/ASP
    asp.net Image Gallery: http://cutesoft.net/ASP.NET+Image+Gallery/default.aspx
    Live Support: http://cutesoft.net/live-support/default.aspx

  •  11-25-2005, 4:32 PM 13039 in reply to 13033

    Re: Get selected text and rewrite it

    Thanks a lot.  I found another way:
       var editor1 = document.getElementById('CE_Editor_ID');
       //Get the editor selection
       var editor1doc = editor1.GetDocument();
       if (editor1doc.selection=="Text")
       {
         selectedhtml = editor1doc.selection.createRange().htmlText;
         alert(selectedhtml);
       }

    However function PasteHTML(html) helps only if I want to replace the whole HTML text in editor. How about "replace selected text with my own"? Thank you in advance.
  •  11-25-2005, 4:43 PM 13041 in reply to 13039

    Re: Get selected text and rewrite it

     
     
      if (editor1doc.selection=="Text")
     
    Should be
     
     if ((editor1doc.selection).type=="Text")
     
     

    asp.net Chat http://cutesoft.net/ASP.NET+Chat/default.aspx
    Web Messenger: http://cutesoft.net/Web-Messenger/default.aspx
    asp.net wysiwyg editor: http://cutesoft.net/ASP.NET+WYSIWYG+Editor/default.aspx
    asp wysiwyg html editor: http://cutesoft.net/ASP
    asp.net Image Gallery: http://cutesoft.net/ASP.NET+Image+Gallery/default.aspx
    Live Support: http://cutesoft.net/live-support/default.aspx

  •  11-25-2005, 4:47 PM 13042 in reply to 13041

    Re: Get selected text and rewrite it

    >>However function PasteHTML(html) helps only if I want to replace the whole HTML text in editor. How about "replace selected text with my own"? Thank you in advance.
     
    PasteHTML(html)  only replace selected text.
     
    You can test here:
     
     
    Select a piece of text then click the "Insert HTML" button.
     
     

    asp.net Chat http://cutesoft.net/ASP.NET+Chat/default.aspx
    Web Messenger: http://cutesoft.net/Web-Messenger/default.aspx
    asp.net wysiwyg editor: http://cutesoft.net/ASP.NET+WYSIWYG+Editor/default.aspx
    asp wysiwyg html editor: http://cutesoft.net/ASP
    asp.net Image Gallery: http://cutesoft.net/ASP.NET+Image+Gallery/default.aspx
    Live Support: http://cutesoft.net/live-support/default.aspx

  •  11-25-2005, 4:49 PM 13043 in reply to 13041

    Re: Get selected text and rewrite it

    How about my question?

    >However function PasteHTML(html) helps only if I want to replace the whole HTML text in editor. How about "replace selected text with my own"? Thank you in advance.
    Ok. I can get the whole text of editor and then replace selected text and afterwards place this text again to editor. However will it be the right way? For example HTML editor may contain the same text which was not selected and in this case it will be replaced as well. So, please give me the right way how to replace selected text on my own. Thanks a lot.  
     
     
  •  11-25-2005, 4:52 PM 13044 in reply to 13043

    Re: Get selected text and rewrite it

    Sorry. I can see the answer from you. Please disregard my last question. Thank you a lot...
  •  03-19-2009, 12:32 AM 50058 in reply to 13033

    Re: Get selected text and rewrite it

    Hi,
     
    sir the PasteHTML method is not working properly for IE(6,7) in my case. I am selects a image and want to replace it with
                      varHtml = ("beforeimage" + image + "afterimage"),
     
    so im using the PasteHTML method. But it dont replace the image with varHtml, it appends the varHtml next to the image, so it becomes
                     image + ("beforeimage" + image + "afterimage")
     
    here image is my image tag <img height="200" alt="" src="http://cutesoft.net/LabTrackPNV4/NotebookImages/Demo.7.1/Winter.jpg" width="200" border="1" />
     
    This is working properly for FF and Safari, but not on IE(6,7).
     
    Here is the javascipt code for it
     
               function CE_LineOutSelectionHTML() {
                     var editor1 = document.getElementById('<%=Editor1.ClientID%>');
                     var editor1doc = editor1.GetDocument();
                
                     var selectedhtml = editor1doc.selection.createRange().htmlText;
                     var html = "**BEFORESELECTION**" + selectedhtml + "**AFTERSELECTION**";
                     editor1.PasteHTML(html);
                     return;
              }
     
     
    Please look into this guide us.
     
    Thanks in advance.
    Filed under:
  •  03-20-2009, 1:32 AM 50112 in reply to 50058

    Re: Get selected text and rewrite it

    Hi Amrutas ,
     
    Try this code:
     

      function CE_LineOutSelectionHTML() {

                     var editor1 = document.getElementById('<%=Editor1.ClientID%>');
                     var editor1doc = editor1.GetDocument();
                     var selectedhtml;
                     if(editor1doc.selection.type=="Control" )
                     {
                        selectedhtml = editor1doc.selection.createRange().item(0).outerHTML;
                     }
                     else
                     {
                     selectedhtml = editor1doc.selection.createRange().htmlText;
                     }
                     var html = "**BEFORESELECTION**" + selectedhtml + "**AFTERSELECTION**";
                     editor1.PasteHTML(html);
                     return;
              }

     
    Regards,
     
    Ken
View as RSS news feed in XML