<div id='mydiv'>div related text from the text div</div>
<textarea id='testtext' rows='3' cols='45' >Dear Freddy,
I have traveled all over with my bag. Now it is up to you to water them.<br/>It is time for all the cows to come home so I will attend to that next.<p>Johnny Appleseed</textarea>
<div class='results'>You Selected:
<div id='youselected'></div>
</div>
// create my namespace
if (!window.Schultheiss) {
Schultheiss = {};
}
//Add TextSelector to my namespace
Schultheiss.TextSelector = {};
//Add getSelected function to my namespace
Schultheiss.TextSelector.getSelected = function() {
var tx = '';
if (window.getSelection) {
tx = window.getSelection();
} else if (document.getSelection) {
tx = document.getSelection();
} else if (document.selection) {
tx = document.selection.createRange().text;
}
return tx;
};
Schultheiss.TextSelector.mouseup = function() {
var st = Schultheiss.TextSelector.getSelected();
if (st === ' ') {
st = ' ';
}
if (st !== '') {
$('#youselected').html(st);
}
};
$(document).ready(function() {
// bind to document, could bind to other more specific if needed
$(document).bind("mouseup", Schultheiss.TextSelector.mouseup);
//example of binding more specific $('#testtext').bind("mouseup", Schultheiss.TextSelector.mouseup);
});
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.