Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
197
rated 0 times [  199] [ 2]  / answers: 1 / hits: 19261  / 14 Years ago, mon, march 7, 2011, 12:00:00
function selected() {
var selObj = window.getSelection();
}




This function returns selected text from a webpage. How do return the html of a selected area. Is this possible to do with an <img> and an <a> tag?





Here's the list of functions:

https://developer.mozilla.org/Special:Tags?tag=DOM&language=en


More From » jquery

 Answers
400

The following will do this 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;
}

[#93392] Saturday, March 5, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
marques

Total Points: 366
Total Questions: 108
Total Answers: 111

Location: Burundi
Member since Wed, Nov 25, 2020
4 Years ago
marques questions
;