Hey
Is is possible to trigger a link to select a single line inside textarea. If it is, how?
Martti Laine
Hey
Is is possible to trigger a link to select a single line inside textarea. If it is, how?
Martti Laine
A similar problem is described here.
In summary, if you know the starting and ending index (or the starting and ending values):
<script type="text/javascript">
function createSelection(field, start, end) {
if (field.createTextRange) {
var selRange = field.createTextRange();
selRange.collapse(true);
selRange.moveStart('character', start);
selRange.moveEnd('character', end - start);
selRange.select();
} else if (field.setSelectionRange) {
field.setSelectionRange(start, end);
} else if (field.selectionStart) {
field.selectionStart = start;
field.selectionEnd = end;
}
field.focus();
}
function select() {
var textArea = document.getElementById('one');
var startIndex = textArea.value.indexOf("help");
var endIndex = textArea.value.indexOf("do") + "do".length;
createSelection(textArea, startIndex, endIndex);
}
</script>
<a href="#" onclick="select();">Select Text</a>
<br />
<textarea id="one" rows="5" cols="25">Please help me do this!</textarea>
To retrieve the value of the selected text, I would recommend jquery-fieldselection [http://github.com/localhost/jquery-fieldselection]