0

My code:

<p id="p">
    123<span>abc</span>456
</p>
<script>
    document.getElementById("p").onmouseup=function(){
         if (window.getSelection) {
             //code
         }
         else if (document.selection) {   
             alert(document.selection.createRange().htmlText)  //IE6 7 8
         }
    }
</script>

what to write at "//code" that I could get the same htmlText in chrome or FF ?

xiaomo
  • 31
  • 1
  • 5
  • https://developer.mozilla.org/en-US/docs/DOM/window.getSelection – salexch Jan 26 '13 at 08:14
  • This is an exact duplicate of [How to get selected html text with javascript?](http://stackoverflow.com/questions/5643635/how-to-get-selected-html-text-with-javascript). Please close. – Dan Dascalescu Jun 01 '15 at 15:33

1 Answers1

2

http://jsfiddle.net/kyP83/

document.getElementsByTagName('p')[0].onmouseup = function() {
    if (typeof window.getSelection === 'function') {
        //code
        var selObj = window.getSelection(); 
        alert(selObj);
        var selRange = selObj.getRangeAt(0);             
    } else if (document.selection) {   
        alert(document.selection.createRange().htmlText)  //IE6 7 8
    }
}
salexch
  • 2,644
  • 1
  • 20
  • 17