Window.getSelection return html

function selected() { var selObj = window.getSelection(); } 


This function returns the selected text from the web page. How to return html for the selected area. Is it possible to do this with the <img> and <a> tags?


Here is a list of functions:
https://developer.mozilla.org/Special:Tags?tag=DOM&language=en

+3
source share
1 answer

The following will be done in all major browsers and is an exact duplicate of this answer :

 function getSelectionHtml() { var html = ""; if (typeof window.getSelection != "undefined") { var sel = window.getSelection(); if (sel.rangeCount) { var container = document.createElement("div"); for (var i = 0, len = sel.rangeCount; i < len; ++i) { container.appendChild(sel.getRangeAt(i).cloneContents()); } html = container.innerHTML; } } else if (typeof document.selection != "undefined") { if (document.selection.type == "Text") { html = document.selection.createRange().htmlText; } } return html; } 
+19
source

Source: https://habr.com/ru/post/1237925/


All Articles