PDA

View Full Version : getselection() - textselection?



Nile
03-02-2008, 12:49 AM
I know how to use this function...
But I only want this function to work on a textarea...
How would I do that!? Also I've tried like document.textname and also getElementById please help! :o

Nile
03-02-2008, 05:58 PM
No one?

Medyman
03-02-2008, 06:05 PM
http://www.squarefree.com/2006/05/06/finding-the-textarea-selection/

Looks like you'll have to have browser specific code as there are different techniques for IE vs Firefox

Nile
03-02-2008, 06:08 PM
Thanks So Much!
But when I do selectionEnd it just prints out how many characters I put..

Felix Riesterer
03-15-2008, 11:52 AM
Why don't you explicitly say for which browser you need which functionality?
You have already been given a hint that there are major differences between the different browsers' implementations of the selection mechanism...

Master_script_maker
03-15-2008, 01:43 PM
this code will check what text is selected in the text box every .2 seconds and output it to a span.

<span id="output">No Text Selected</span>
<hr>
<form action="#" method="get">
<textarea id="select" cols="40" rows="5">1234567890
abcdefghijklmnopqrstuvwxyz</textarea><br>
</form>
<script type="text/javascript">
var int=setInterval(check, 200);
function check() { document.getElementById("output").innerHTML = getSelectionInfo(); }

function getSelectionInfo()
{
var rv = "";
var x;
x = document.getElementById("select");
if(!document.selection) { //if not IE
rv = x.value.substr(x.selectionStart, x.selectionEnd - x.selectionStart);
} else {
rv = document.selection.createRange().text;
}
if (rv == null || rv == "") {
rv="No Text Selected";
}
return rv;
}

</script>