CuteEditor for .NET

Retrieves the HTML contained within the selection range

Retrieves the HTML contained within the range

In order to get the HTML contained within the selection range, you need to use the following code:

  1. function getSelectedHTML()      
  2. {         
  3.       var rng=null,html="";            
  4.          
  5.       // get the active editor document   
  6.       var editdoc = editor1.GetDocument();   
  7.   
  8.       // get the active editor window    
  9.       var editwin = editor1.GetWindow();      
  10.       if (document.selection && document.selection.createRange)      
  11.       {         
  12.             rng=editdoc.selection.createRange();      
  13.            if( rng.htmlText )       
  14.            {       
  15.               html=rng.htmlText;       
  16.            }       
  17.            else if(rng.length >= 1)       
  18.            {       
  19.               html=rng.item(0).outerHTML;       
  20.            }      
  21.       }      
  22.       else if (window.getSelection)      
  23.       {      
  24.             rng=editwin.getSelection();      
  25.             if (rng.rangeCount > 0 && window.XMLSerializer)      
  26.             {      
  27.                   rng=rng.getRangeAt(0);      
  28.                   html=new XMLSerializer().serializeToString(rng.cloneContents());      
  29.             }      
  30.       }      
  31.       return html;      
  32. }