lk******@geocities.com wrote:
I've got a function, you can see it below, that is being called
onmouseup in the textarea on my main form. The idea is to find a
selection if possible and store that text in a global variable. I can't
get this to work in any browser on a Mac, though it works alright on a
PC. What am I missing?
That document.selection and element.selectionStart don't work in
Safari or IE 5.2.
However, element.selectionStart does work in Firefox on Mac but if you
select from the start of the text, selectionStart returns undefined so
your test:
if ('number' == typeof myField.selectionStart) {
will fail.
Your method will also fail if the user selects all the text by clicking
in the field and uses Ctrl+A. It also stops double-clicking in the
field to select all the text.
The following play code works in Firefox 1.0 on Mac OS X (but not in
either Safari 1.0.3 or IE 5.2). It deals with selectionStart being
undefined if all the selection starts with the first character. Note
that if the user doesn't select anything, the start and end will have
the same value, and if the cursor is before the first character, both
selectionStart and selectionEnd will be undefined.
If the user selects some text, then clicks in the field, the function
will return the same value as if some text was still selected. I think
that qualifies as unreliable, but it's up to you.
<html><head><title>play</title>
<script type="text/javascript">
function setFieldSelectionValue(myField) {
(document.selection)?
alert('document.selection works') :
alert('document.selection doesn\'t work');
var start, end;
if(myField.selectionStart || myField.selectionEnd) {
if (myField.selectionStart) {
start = myField.selectionStart
} else {
start = 0;
}
end = myField.selectionEnd;
alert('Started at ' + start + '\nEnded at ' + end);
} else {
alert('selectionStart and selectionEnd not supported');
}
}
</script>
</head><body>
<form action="">
<textarea onmouseup="
setFieldSelectionValue(this);">Fred is a very fine fellow</textarea>
</form>
</body></html>
--
Fred