Source Code:
(back to article)
Submit
Result:
Report an issue
<!DOCTYPE html> <html> <head> <title>Title of the Document</title> </head> <body> <p id="p">Select: <b>bold</b> and <i>italic</i></p> Clone: <span id="cloneId"></span> <br> Text: <span id="textId"></span> <script> document.onselectionchange = function() { let select = document.getSelection(); cloneId.innerHTML = textId.innerHTML = ""; // Clone DOM nodes from ranges (multiple selection is supported here) for(let i = 0; i < select.rangeCount; i++) { cloneId.append(select.getRangeAt(i).cloneContents()); } // Get as text textId.innerHTML += select; }; </script> </body> </html>