Get Selected Text

Last post 09-06-2009, 10:58 PM by humarashid. 3 replies.
Sort Posts: Previous Next
  •  09-04-2009, 3:15 AM 55365

    Get Selected Text

    Hi Everyone!!!!!!!1
     
    I want to get selected text from rich text box and for rich textbox im using cute Editor for net. For Example if  a text say "I m the selected text" is selected by user how can i get the selected string..
     
     
    Any help will be greatly appreciated......Please
     
    Regards
    Huma
  •  09-04-2009, 5:00 AM 55366 in reply to 55365

    Re: Get Selected Text

    Hi humarashid,
     
    Try the example below:
     
    1. <%@ Page Language="C#" %>   
    2.   
    3. <%@ Register Namespace="CuteEditor" Assembly="CuteEditor" TagPrefix="CE" %>   
    4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">   
    5. <html xmlns="http://www.w3.org/1999/xhtml">   
    6. <head id="Head1" runat="server">   
    7.     <title>Untitled Page</title>   
    8. </head>   
    9. <body>   
    10.     <form id="form1" runat="server">   
    11.         <div>   
    12.             <CE:Editor ID="editor1" runat="server">   
    13.             </CE:Editor>   
    14.             <input type="button" value="show selected message" onclick="show()" />   
    15.         </div>   
    16.     </form>   
    17. </body>   
    18. </html>   
    19.   
    20. <script type="text/javascript">   
    21. function show()   
    22. {   
    23. alert(getSelectedHTML());   
    24. }   
    25. var editor1=document.getElementById('<%= editor1.ClientID %>');   
    26. function getSelectedHTML(){   
    27.       // get the active editor window   
    28.       var editwin = editor1.GetWindow();   
    29.   
    30.   
    31.       // get the active editor document   
    32.       var editdoc = editor1.GetDocument();   
    33.   
    34.       var rng=null,html="";    
    35.       if (document.selection && document.selection.createRange){   
    36.         rng=editdoc.selection.createRange();   
    37.         html=rng.htmlText||"";   
    38.       }else if (window.getSelection){   
    39.         rng=editwin.getSelection();   
    40.   
    41.         if (rng.rangeCount > 0 && window.XMLSerializer){   
    42.           rng=rng.getRangeAt(0);   
    43.           html=new XMLSerializer().serializeToString(rng.cloneContents());   
    44.         }   
    45.       }   
    46.       return html;   
    47.  }   
    48.   
    49. </script>  
    Regards,
     
    Ken
  •  09-06-2009, 10:28 PM 55400 in reply to 55366

    Re: Get Selected Text

    Thankyou so much Kenneth!!!!!!!!
     
    But there is one problem that var editor1 thats get the elementbyid returns null thats why it does not show selected text. I m using IE6 as my web browser.
     
     
    Thanks
  •  09-06-2009, 10:58 PM 55402 in reply to 55400

    Re: Get Selected Text

    Problem Solved i was making mistake i the placement of script...
     
     
    Thanks Kenneth.......
View as RSS news feed in XML