Does anybody have a cross browser JS function to get selected text

Last post 02-08-2008, 10:10 AM by Flintstone. 2 replies.
Sort Posts: Previous Next
  •  02-08-2008, 7:37 AM 36890

    Does anybody have a cross browser JS function to get selected text

    I am trying to create a cross browser JS function to get selected text ... does anybody have anything better than what I am using?

    function GetSelectedText()
       {
       var retval='';
     
       <%If mozilla Then%>
             sel=ce.GetSelection();
             retval=sel.focusNode.textContent.substr(sel.anchorOffset,sel.focusOffset-sel.anchorOffset);
       <%ElseIf IE Then%>
             sel=ce.GetSelection().createRange();
             retval=sel.htmlText;
       <%End If%>
     
       return retval;
       }
  •  02-08-2008, 7:46 AM 36891 in reply to 36890

    Re: Does anybody have a cross browser JS function to get selected text

    Please use the following code:
     
     
     
      function getSelectedHTML(){
          var rng=null,html="";

          if (document.selection && document.selection.createRange){
            rng=editdoc.selection.createRange();
            html=rng.htmlText||"";
          }else if (window.getSelection){
            rng=editwin.getSelection();

            if (rng.rangeCount > 0 && window.XMLSerializer){
              rng=rng.getRangeAt(0);
              html=new XMLSerializer().serializeToString(rng.cloneContents());
            }
          }
          return html;
        }


    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

  •  02-08-2008, 10:10 AM 36894 in reply to 36891

    Re: Does anybody have a cross browser JS function to get selected text

    Thanks, that really got me started, I have fixed the errors and added a keepHTML parameter so that I can choose whether I want just plain text or not.
     
    function HTMLFieldGetSelectedText(keepHTML)
        {
        var rng=null,retval='';
        var ce=document.getElementById('CE_Editor_ID');
       
        if(ce&&ce.IsReady)
            {
            if (ce.GetSelection && ce.GetSelection().createRange)
                {
                rng=ce.GetSelection().createRange();
                retval=keepHTML?rng.htmlText||'':rng.text;
                }
            else if (ce.GetSelection)
                {
                rng=ce.GetSelection();
               
                if (rng.rangeCount > 0 && window.XMLSerializer)
                    {
                    rng=rng.getRangeAt(0);
                    retval=keepHTML?new XMLSerializer().serializeToString(rng.cloneContents()):rng.cloneContents().textContent;;
                    }
                }
            }
       
        return retval;
        }
     

View as RSS news feed in XML