468,107 Members | 1,347 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,107 developers. It's quick & easy.

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 7115


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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Krzysztof Kujawski | last post: by
6 posts views Thread by stuart | last post: by
2 posts views Thread by Mark Szlazak | last post: by
5 posts views Thread by aqualizard | last post: by
2 posts views Thread by dennis.sprengers | last post: by
1 post views Thread by Solo | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.