473,703 Members | 3,348 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,h aystack,start) {
var element = document.getEle mentById(haysta ck);
index = element.value.i ndexOf(needle,s tart);
element.setSele ctionRange(inde x, index+needle.le ngth);
}

Thanks for any insight,
Keith

May 31 '06 #1
4 7798


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,h aystack,start) {
var element = document.getEle mentById(haysta ck);
index = element.value.i ndexOf(needle,s tart);
element.setSele ctionRange(inde x, index+needle.le ngth);


You can manipulate
element.scrollT op
element.scrollL eft
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,h aystack,start) {
var element = document.getEle mentById(haysta ck);
index = element.value.i ndexOf(needle,s tart);
element.setSele ctionRange(inde x, index+needle.le ngth);


You can manipulate
element.scrollT op
element.scrollL eft
to scroll the textarea.
--

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


May 31 '06 #3
sw********@gmai l.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,h aystack,start) {
var element = document.getEle mentById(haysta ck);
index = element.value.i ndexOf(needle,s tart);
element.setSele ctionRange(inde x, index+needle.le ngth);


You can manipulate
element.scrollT op
element.scrollL eft
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.select ionStart), 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
..selectionStar t/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>Te xtarea 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.getEle mentById(id); }
function search(needle,h aystack,start) {
var i, element = elem(haystack);
index = element.value.i ndexOf(needle,s tart||0);
if (index<0) {
window.status = "Not found: " + needle;
window.setTimeo ut (function(){win dow.status=''}, 4000); }
else {
element.setSele ctionRange((i=i ndex+needle.len gth)-1,i);
var ev = document.create Event ('KeyEvents');
ev.initKeyEvent ('keypress', true, true, window,
false, false, false, false, 0,
element.value.c harCodeAt(i-1));
element.dispatc hEvent(ev); // causes the scrolling
element.setSele ctionRange(inde x, 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=e lement.value; // this line has value
element.setSele ctionRange(0, needle.length); }

The middle line will cause the scrolling to be reset. You could
instead use:
element.scrollT op = 0; element.scrollL eft = 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
6846
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 value+='' how to move cursor to position after ] and before [
6
4631
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 anyone help. Thanks Stuart
2
11233
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(); var safari = ((agt.indexOf('safari') != -1) && (agt.indexOf('mac') != -1))? true:false; var opera = (window.opera)? true:false;
5
10618
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" of the word "apple", using a mouse. Is there a way to have the (usally blue) highlight expand to the character to the left and right of the original highlight, which is to say, highlight the entire word?
1
2298
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 scrolling. Here is my HTML file ... FORM name="TraceFile"> <TextArea Name = "commentbox" ROWS="8" COLS="70"></TextArea>
2
2899
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 selectie ingesloten door \ en \, net als hier op GoT. Wat ik nu probeer is, om de B-knop uit de toolbar te laten oplichten als de cursor op een woord staat dat omgeven is door B-tags: I'm trying to write my own UBB editor. If a user types...
3
2692
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 all the rest of it. I have noticed that vBulletin has the best formatting buttons (and no, I'm not talking about the WYSIWYG editor, I know that's way beyond me) so I was thinking of something similar (if I get bold, italic and underlined working, I'm...
1
2183
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) User selects a selection from dropdown1 which appears in the textarea 2) User types whatever they want after this in the textarea 3) User selects a selection from dropdown2, which appears in the textarea underneath what's already there. It doesn't...
0
8759
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8670
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9122
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8966
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6588
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4687
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3125
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2070
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.