473,395 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Textarea text selection problem in Firefox

The following code fails in Firefox to get at selected text in the
right-side textarea. Any help would be appreciated.

<html>
<head>
<script>
var agt = navigator.userAgent.toLowerCase();
var safari = ((agt.indexOf('safari') != -1) && (agt.indexOf('mac') !=
-1))? true:false;
var opera = (window.opera)? true:false;
var ie = (document.all && !safari && !opera)? true:false;
var moz = (document.getElementById && !safari && !opera && !ie)?
true:false;

var i, str, lineHeight = 16;

function setScrollBar (ta, sb) {
var textareaBorderWidth = 2;
sb.style.left = ta.offsetParent.offsetParent.offsetLeft +
ta.offsetParent.offsetLeft + ta.offsetLeft + ta.clientWidth +
textareaBorderWidth + 1;
sb.style.top = ta.offsetParent.offsetParent.offsetTop +
ta.offsetParent.offsetTop + ta.offsetTop + textareaBorderWidth;
sb.style.height = ta.clientHeight;
sb.style.width = 16;
}

function syncScrollBars (sb1, sb2, ta1, ta2) {
sb1.onscroll = function() {
ta1.scrollTop = this.scrollTop;
ta2.scrollTop = this.scrollTop;
sb2.scrollTop = this.scrollTop;
}
sb2.onscroll = function() {
ta1.scrollTop = this.scrollTop;
ta2.scrollTop = this.scrollTop;
sb1.scrollTop = this.scrollTop;
}
}

function syncTextareas (ta1, ta2) {
ta1.onscroll = function (evt) {
ta2.scrollTop = this.scrollTop;
}
ta2.onscroll = function (evt) {
ta1.scrollTop = this.scrollTop;
}
}
onload = function () {
if (!moz) syncTextareas (document.getElementById('ta1'),
document.getElementById('ta2'));
else {
syncScrollBars (document.getElementById('sb1'),
document.getElementById('sb2'), document.getElementById('ta1'),
document.getElementById('ta2'));
setScrollBar(document.getElementById('ta1'),
document.getElementById('sb1'));
setScrollBar(document.getElementById('ta2'),
document.getElementById('sb2'));
}
}
</script>
</head>
<body>
<form name="f">
<table border=0 cellpadding=0 cellspacing=0>
<tr valign="top">
<td>
<textarea id="ta1" rows=10 cols=40>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20</textarea>
</td>
<td>&nbsp;</td>
<td>
<textarea id="ta2" rows=5 cols=40>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20</textarea>
<input type="button" value="show selection"
onclick="var ta = this.form.ta2;
if (document.selection) {
var range = document.selection.createRange();
if (range.parentElement() == ta)
alert(range.text);
}
else if (ta.selectionStart)
alert(ta.value.substring(ta.selectionStart, ta.selectionEnd));
else alert('no selection detected');"

</td>
</tr>
<tr>
</table>
</form>

<script>
if (moz) {
str = "<div id=\"sb1\" style=\"position: absolute; overflow:
auto;\">";
for (i = 0; i <
Math.round((document.getElementById('ta1').scrollH eight - 70) /
lineHeight); i++)
str += '<br>';
str += "<\/div>";
str += "<div id=\"sb2\" style=\"position: absolute; overflow:
auto;\">";
for (i = 0; i < Math.round(document.getElementById('ta2').scrollHe ight
/ lineHeight); i++)
str += '<br>';
str += "<\/div>";
document.write(str);
}
</script>
</body>
</html>

Jul 23 '05 #1
2 11189
There seems to be a problem with selectionStart when selections include
the first character position of the first line of the textarea. If you
start your selection at any point after that then things work fine. One
could add a space to the beginning of the test then make selections
that start any place after that but then you loose the ability to use
"select all". Does anyone know what going on with this? Here's a
smaller sample script with the same issue.

<html>
<head>
</head>
<body>
<form name="F">
<textarea name="TA" rows="5" cols="40">
Textarea text selection test.
Test for textarea text selection.
</textarea>
<input type="button" value="show selection"
onclick="var ta = this.form.TA;
if (document.selection) {
var range = document.selection.createRange();
if (range.parentElement() == ta)
alert(range.text);
}
else if (ta.selectionStart)
alert(ta.value.substring(ta.selectionStart, ta.selectionEnd));"

</form>
</body>
</html>

Jul 23 '05 #2
Never mind I found the problem! Changing my code from
"if (ta.selectionStart)"
to
"if (ta.selectionEnd - ta.selectionStart)"
solves the issue.

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Gernot Frisch | last post by:
Hi, I want to be able to select 3 rows of a textarea, and when pressing "Tab/shift Tab" indent the source code edited. Can I do this with JavaScript? -- -Gernot int main(int argc, char**...
1
by: tranky | last post by:
Hola boys, i've a question for you! It's possible to highlight some text inside a textarea? (not all text, but a portion!) For example, i've this text inside a textarea. INSERISCI TESTO i...
4
by: Keith Bentrup | last post by:
Hi all, I wrote a simple search function to find text in a textarea where not all the text is visible (ie. the text box displays 10 lines but there may be more than 1000 lines to search). I can...
2
by: Daniel Pitts | last post by:
Why doesn't this work? I create an object which is supposed to handle the selection in both IE and Firefox, but everytime I call getText() in firefox, I get the whole textarea, not just the...
4
by: jim | last post by:
Hi, I have a form that accepts free text from users in a single textarea field. I would like for a select list of words (15 or so) to autopopulate as the user types. For example, instead of...
2
by: dennis.sprengers | last post by:
Ik ben bezig met een eigen UBB editor. Als iemand aan het typen is, zorgt CTRL-B voor een \-tag en nogmaals CTRL-B voor een \ tag. Als je eerst een selectie maakt en dan CTRL-B drukt, wordt de...
2
realin
by: realin | last post by:
hi guys, I am trying to make a simple text editor for IE and firefox using javascript. I want to know how can i bold the selected text and otehr stuff like italics and all. cause in IE i was able...
7
by: =?ISO-8859-1?Q?=22=C1lvaro_G=2E_Vicario=22?= | last post by:
I need to emulate the missing "maxlegth" attribute in "textarea" fields but all my Google searches either lead to obsolete scripts that overwrite the "value" property (thus losing caret position)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.