/ Published in: JavaScript

Again, as part of the DCODR development I had to find a decent way to get the selection HTML
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
getSelectionHTML = function () { var userSelection; if (window.getSelection) { // W3C Ranges userSelection = window.getSelection (); // Get the range: if (userSelection.getRangeAt) var range = userSelection.getRangeAt (0); else { var range = document.createRange (); range.setStart (userSelection.anchorNode, userSelection.anchorOffset); range.setEnd (userSelection.focusNode, userSelection.focusOffset); } // And the HTML: var clonedSelection = range.cloneContents (); var div = document.createElement ('div'); div.appendChild (clonedSelection); return div.innerHTML; } else if (document.selection) { // Explorer selection, return the HTML userSelection = document.selection.createRange (); return userSelection.htmlText; } else { return ''; } };
Comments
