473,748 Members | 5,232 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 7804


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
6848
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
4636
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
11235
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
10619
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
2299
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
2903
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
2699
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
2185
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
8831
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
9548
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9374
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...
1
9325
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9249
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
4607
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4876
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2787
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
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.