<p>Nisi tincidunt! Enim scelerisque, elementum? Ut turpis, ultricies ac? Et elit! Aliquet nascetur elementum placerat, nec magna nunc placerat, duis elementum adipiscing parturient. Integer mid ridiculus lacus in sed, a ac! Mattis pulvinar tincidunt habitasse tristique mus duis aliquam, placerat vut ultrices elit penatibus turpis in placerat! Elementum non sagittis, amet nascetur turpis, a et ridiculus ut platea dolor. Adipiscing urna ultricies augue, sed adipiscing! Facilisis magna rhoncus ultricies! Nisi penatibus vel turpis elementum tempor in vut placerat pulvinar, arcu odio vel cras amet mauris habitasse elementum tristique tortor non magna? Pulvinar parturient! Vut vut quis lorem parturient pulvinar porttitor magna, eu amet lacus? Aliquet pulvinar sed tempor, est? Massa, nec scelerisque nunc quis ac! Vut placerat placerat lacus.</p>
<div>Adipiscing urna ultricies augue, sed adipiscing! Facilisis magna rhoncus ultricies! Nisi penatibus vel turpis elementum tempor in vut placerat pulvinar, arcu odio vel cras amet mauris habitasse elementum tristique tortor non magna? Pulvinar parturient! Vut vut quis lorem parturient pulvinar porttitor magna, eu amet lacus? Aliquet pulvinar sed tempor, est? Massa, nec scelerisque nunc quis ac! Vut placerat placerat lacus.</div>
JavaScript
function selectCallback(selectionParentElement) {
alert('Text selected on the ' + selectionParentElement.nodeName.toLowerCase() + ' element');
}
var mouseOrKeyUpHandler;
if (typeof window.getSelection != "undefined") {
// Non-IE
mouseOrKeyUpHandler = function() {
var sel = window.getSelection();
if (sel.rangeCount > 0) {
var range = sel.getRangeAt(0);
if (range.toString()) {
var selParentEl = range.commonAncestorContainer;
if (selParentEl.nodeType == 3) {
selParentEl = selParentEl.parentNode;
}
selectCallback(selParentEl);
}
}
};
} else if (typeof document.selection != "undefined") {
// IE
mouseOrKeyUpHandler = function() {
var sel = document.selection;
if (sel.type == "Text") {
var textRange = sel.createRange();
if (textRange.text != "") {
selectCallback(textRange.parentElement());
}
}
};
}
$(document).bind('mouseup', mouseOrKeyUpHandler);
$(document).bind('keyup', mouseOrKeyUpHandler);
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.