field-selection
Version:
Get text selection or replace selected text in input field or textarea.
42 lines (33 loc) • 1.33 kB
HTML
<html>
<head>
<title>Field selection demo</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript" src="../lib/field-selection.js"></script>
</head>
<body>
<button id="get">get selection</button>
<button id="replace">replace selection</button>
<br />
<textarea id="textarea" style="width: 300px; height: 200px;">
E nex incontinencia, exigo era Palus sum iam magnificabiliter loci, sal incurro, dux necessarius Negotium os orbis, era alatus ineo, vel loquor, hic sed, Viva tam. Ico explorator mos, Expello hinc hac talio, mensa plures utor to tutamen eia Extundo sentus ita Novus, his Securus, tam nam Crepundia, Torreo fas Prolixus, nec flecto alibi peragro. Nam Deficio contradictio ops Posco laeto aeger Freno .
</textarea>
<br /><br />
<input type="text" id="text" />
<script>
(function() {
var $elem;
$('input, textarea').focus(function(){
$elem = $(this);
});
$('#get').on('click', function() {
alert($elem.fieldSelection().text);
window.console && console.log($elem.fieldSelection());
});
$('#replace').on('click', function() {
$elem.fieldSelection('<REPLACED>');
});
}());
</script>
</body>
</html>