GetSelection
HTML
<link rel="stylesheet" href="http://twitter.github.com/bootstrap/1.4.0/bootstrap.min.css">
<script src="https://raw.github.com/JamesMGreene/jquery.textSelect/gh-pages/src/jquery.textSelect.js"></script>
<script src="https://raw.github.com/documentcloud/underscore/master/underscore.js"></script>
<h3>Selected Text:</h3>
<p id="result"></p>
<p id="result2"></p>
<br/>
<div style="position:absolute; top:25%; left:25%; margin: 0px auto 0px auto; width:50% height:50%; z-index:1;">
<textarea id="copytextarea" rows=10 style="display:none"></textarea>
</div>
<p>
<input type="button" id="actionButton" value="Show the selected text above" />
<input type="button" value="select h1" onclick="select_cold();">
<br/>Paste here:
<input type="textarea" rows="2" value="">
</p>
<!-- start slipsum code -->
<div id="slipsum">
<h1>Is she dead, yes or no?</h1>
<p>Do you see any Teletubbies in here? Do you see a slender plastic tag clipped to my shirt with my name printed on it? Do you see a little Asian child with a blank expression on his face sitting outside on a mechanical helicopter that shakes when you put quarters in it? No? Well, that's what you see at a toy store. And you must think you're in a toy store, because you're here shopping for an infant named Jeb.</p>
<h1 id="cold">So, you cold?</h1>
<p>The path of the righteous man is beset on all sides by the iniquities of the selfish and the tyranny of evil men. Blessed is he who, in the name of charity and good will, shepherds the weak through the valley of darkness, for he is truly his brother's keeper and the finder of lost children. And I will strike down upon thee with great vengeance and furious anger those who would attempt to poison and destroy My brothers. And you will know My name is the Lord when I lay My vengeance upon thee.</p>
<h1 style="background-color: #9CACC9">I'm serious as a heart attack</h1>
<p>Do you see any Teletubbies in here? Do you see a slender plastic tag...
JavaScript
function selectElementContents(el) {
var body = document.body,
range, sel;
if (document.createRange && window.getSelection) {
range = document.createRange();
sel = window.getSelection();
sel.removeAllRanges();
try {
range.selectNodeContents(el);
sel.addRange(range);
} catch (e) {
console.log(e);
range.selectNode(el);
sel.addRange(range);
}
el.css("border", "3px solid green");
} else if (body.createTextRange) {
range = body.createTextRange();
range.moveToElementText(el);
range.select();
el.css("border", "3px solid red");
}
}
function select_cold() {
var obj = $("#cold");
selectElementContents(obj.get(0));
}
$(function () {
$('#actionButton').click(function () {
var inputs=$( "#secretarea" );
console.log(inputs.length );
inputs.each(function( index ) {
console.log( index + ": " + $( this ).attr('id') + ' = ' + $( this ).val());
});
// Find background-color: #9CACC9;
sel = $('h1').filter(function () {
console.log($(this).text());
console.log($(this).css('background-color'));
return $(this).css('background-color') === 'rgb(156, 172, 201)';
})
$('#result2').text(sel.text());
if (selectedRange) {
$('#result').text(selectedRange.toString());
clearInterval(timer);
}
});
timer = setInterval(getSelectedRange, 150);
});
var timer = null;
var selectedRange = null;
var getSelectedRange = function () {
try {
if (window.getSelection) {
selectedRange = window.getSelection().getRangeAt(0);
} else {
selectedRange = document.getSelection().getRangeAt(0);
}
} catch (err) {}
};
var hiddenArea = null;
function makeReadyToCopy(text) {
//window.prompt("Copy to clipboard:...