xiaomo
xiaomo

Reputation: 31

how to get HTML with "window.getSelection()"

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 ?

Upvotes: 0

Views: 4849

Answers (1)

salexch
salexch

Reputation: 2704

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
    }
}

Upvotes: 2

Related Questions