473,289 Members | 2,155 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,289 software developers and data experts.

scrolling in textarea to selection in FF

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 find the text and select
it using the function below, BUT I can't figure out how to have the
textarea automatically scroll to the selection in Firefox. Any ideas or
suggestions?

function search(needle,haystack,start) {
var element = document.getElementById(haystack);
index = element.value.indexOf(needle,start);
element.setSelectionRange(index, index+needle.length);
}

Thanks for any insight,
Keith

May 31 '06 #1
4 7698


Keith Bentrup wrote:

I can find the text and select
it using the function below, BUT I can't figure out how to have the
textarea automatically scroll to the selection in Firefox. Any ideas or
suggestions?

function search(needle,haystack,start) {
var element = document.getElementById(haystack);
index = element.value.indexOf(needle,start);
element.setSelectionRange(index, index+needle.length);


You can manipulate
element.scrollTop
element.scrollLeft
to scroll the textarea.
--

Martin Honnen
http://JavaScript.FAQTs.com/
May 31 '06 #2
Hi Martin,
Thanks for the quick reply. I'm aware that scrolling is possible with
scrollTop and scrollLeft. The problem is I don't know how to find the
start position of the selection, so that I can actually scroll to it.
Does anyone know of a way to retrieve in pixels (or some other clever
solution) where the selection is so that I can scroll to it???
Thanks,
-K

Martin Honnen wrote:
Keith Bentrup wrote:

I can find the text and select
it using the function below, BUT I can't figure out how to have the
textarea automatically scroll to the selection in Firefox. Any ideas or
suggestions?

function search(needle,haystack,start) {
var element = document.getElementById(haystack);
index = element.value.indexOf(needle,start);
element.setSelectionRange(index, index+needle.length);


You can manipulate
element.scrollTop
element.scrollLeft
to scroll the textarea.
--

Martin Honnen
http://JavaScript.FAQTs.com/


May 31 '06 #3
sw********@gmail.com wrote:
Martin Honnen wrote:
Keith Bentrup wrote:
I can find the text and select
it using the function below, BUT I can't figure out how to have the
textarea automatically scroll to the selection in Firefox. Any ideas or
suggestions?

function search(needle,haystack,start) {
var element = document.getElementById(haystack);
index = element.value.indexOf(needle,start);
element.setSelectionRange(index, index+needle.length);


You can manipulate
element.scrollTop
element.scrollLeft
to scroll the textarea.


Hi Martin,
Thanks for the quick reply. I'm aware that scrolling is possible with
scrollTop and scrollLeft. The problem is I don't know how to find the
start position of the selection, so that I can actually scroll to it.
Does anyone know of a way to retrieve in pixels (or some other clever
solution) where the selection is so that I can scroll to it???


I hope Martin has some insight on this - I'd be mighty interested. I
assume you know that if you do a search in Firefox (ctrl+f), it doesn't
show you what it found in textareas. Unless you press alt+a (select
all).

I can think of two possible approaches to your problem, both messy.
The first (and least tenable one, in my opinion), is to write an
extension that allows you simulate left and up arrow keys. (The
extension is on account of
https://bugzilla.mozilla.org/show_bug.cgi?id=337975 - if that report
gets acknowledged and fixed, then the extension wouldn't be necessary).
Then count how far HOME gets you (with textarea.selectionStart), and
then count up arrow keys till you get to the beginning. You still need
to calculate row heights and fun things like that which you could do by
inserting for just long enough a cloned text area into the dom to get
the appropriate measurements and then delete it. Complicated and
messy. I can give you starting references for the requisite key
simulation if you wish, but it would only work on your personal
machine.

The other possiblilty is to construct a temporary DIV with the exact
characteristics and font of the textarea (since that's what the
textarea is using internally anyway). This allows you to work with
real ranges and perhaps you can get at the necessary information more
easily. This seems more clean to me, though I haven't done it.

In any event, I am interested in what you come up, and would appreciate
seeing the code for it.

Csaba Gabor from Budapest

May 31 '06 #4
Firefox textarea scrolling made simple: Now that
https://bugzilla.mozilla.org/show_bug.cgi?id=303713 is fixed, a much
simpler approach to textarea scrolling is possible (recall that the
problem is to scroll to a given textarea's selection - see
..selectionStart/End). We can make use of the fact that FF scrolls the
textarea to the caret if there is a text change (text change implies
that any prior selection is collapsed). Some notes follow the demo.

Csaba Gabor from Vienna
<html>
<head><title>Textarea scrolling</title></head>
<body bgcolor=yellow style=margin-left:.4in>
<form action='' method=get>
<textarea id=ta name=ta>
This is some initial
text for the purpose of
searching in the textarea
to see how scrolling to a
non currently showing section works
</textarea><br>
Sear<u>c</u>h term: <input type=text id=tbNeedle
name=tbNeedle accesskey=c>
<button type=button accessKey=s
onclick="search(elem('tbNeedle').value,'ta')"
Te<u>s</u>t</button>

</form>
Suggestion:<br>
(1) Don't put focus in the textarea<br>
(2) First search for 'the textarea' (without quotes)<br>
(3) Then search for 'This'
<script type='text/javascript'>
function elem(id) { return document.getElementById(id); }
function search(needle,haystack,start) {
var i, element = elem(haystack);
index = element.value.indexOf(needle,start||0);
if (index<0) {
window.status = "Not found: " + needle;
window.setTimeout (function(){window.status=''}, 4000); }
else {
element.setSelectionRange((i=index+needle.length)-1,i);
var ev = document.createEvent ('KeyEvents');
ev.initKeyEvent('keypress', true, true, window,
false, false, false, false, 0,
element.value.charCodeAt(i-1));
element.dispatchEvent(ev); // causes the scrolling
element.setSelectionRange(index, i);
}
}
</script>
</body></html>
Notes:
(A) You might wish to explicitly test for (!index) in which case you
could have:
else if (!index) {
element.value=element.value; // this line has value
element.setSelectionRange(0, needle.length); }

The middle line will cause the scrolling to be reset. You could
instead use:
element.scrollTop = 0; element.scrollLeft = 0;

(B) The reason for not putting focus into the textarea is on account
of https://bugzilla.mozilla.org/show_bug.cgi?id=332424: textarea
selections made (via javascript) before the textarea has received focus
will be visible even though the textarea does not have focus, which
allows for simpler search changes.

Jun 2 '06 #5

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

Similar topics

3
by: Krzysztof Kujawski | last post by:
Hi Is there posibility to (and how to do this): a) add a string in the current cursor position? b) add string before and after selection in textarea e.g. selection c) if I put with...
6
by: stuart | last post by:
I have two textareas on a HTML page. If a user scrolls in one textarea I want the other textarea to scroll as well. In otherwords I want both textareas to scroll up and down in unison. Can...
2
by: Mark Szlazak | last post by:
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();...
5
by: aqualizard | last post by:
I have searched and searched and searched... Can someone please tell me how (or "if" it is even possible) to change selected text in a textarea? Specifically, say a user has highlighted "ppl"...
1
by: Anirhudra | last post by:
I have a TextArea and there I want to write my tracefile info .... but I want to see the scrolling automatically and to see the last line of my tracefile info within this TextArea without...
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...
3
kendall
by: kendall | last post by:
I'm currently working on a PHP CMS for my school that uses simple forms to update the pages and database. To get it out in use, it will have to be usable by people who don't know that is bold, and...
1
by: beary | last post by:
I need to populate a textarea from drop down box. There are a few "solutions" out there, but they seem to remove the first choice from the textarea when a second selection is made. So I need... 1)...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

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.