setSelectionRange and friends

T

Tim Streater

I'm aiming to replicate what Mac-Eudora does when you start to type in
an e-mail address into as it might be the To: field. If Eudora
recognises that you seem to be typing in a nickname that it can expand,
it:

1) completes the nickname and highlights the extra characters it has
entered

2) makes that whole nickname <strong>, or bold (but not the rest of the
To: line)

3) allows you to continue typing to complete the nickname or type a
comma to accept it and have it expanded

4) as you type, the highlighted area shrinks one char to the right at a
time


In thinking how I might do this, using onkeypress and onkeyup, I come
across setSelectionRange, startSelection and endSelection for use with a
textarea that I think might be useful in this context (found by some
amount of googling). But none of these is documented as far as I can
tell in for example Danny Goodman's or David Flanagan's JavaScript books.

These seem to work in Safari 4 beta and FF3 (both Mac), but I'm
concerned they might be deprecated or not be best suited for my needs -
or that they might be part of a useful and well supported set that for
some reason I'm going to have to discover one item at a time.

Comments/suggestions welcome.
 
T

Tim Down

I'm aiming to replicate what Mac-Eudora does when you start to type in
an e-mail address into as it might be the To: field. If Eudora
recognises that you seem to be typing in a nickname that it can expand,
it:

1) completes the nickname and highlights the extra characters it has
entered

2) makes that whole nickname <strong>, or bold (but not the rest of the
To: line)

3) allows you to continue typing to complete the nickname or type a
comma to accept it and have it expanded

4) as you type, the highlighted area shrinks one char to the right at a
time

In thinking how I might do this, using onkeypress and onkeyup, I come
across setSelectionRange, startSelection and endSelection for use with a
textarea that I think might be useful in this context (found by some
amount of googling). But none of these is documented as far as I can
tell in for example Danny Goodman's or David Flanagan's JavaScript books.

These seem to work in Safari 4 beta and FF3 (both Mac), but I'm
concerned they might be deprecated or not be best suited for my needs -
or that they might be part of a useful and well supported set that for
some reason I'm going to have to discover one item at a time.

Comments/suggestions welcome.

I'm not sure exactly which browsers support setSelectionRange,
startSelection and endSelection on textareas, but IE definitely
doesn't. You'll need to look at TextRange for IE - calling
createTextRange() on a textarea will return you a TextRange that
represents all the text in the textarea. You can then move the
boundaries of this TextRange (using move() and similar methods), and
call its select() method to update the user selection.

Tim
 
T

Tim Streater

Tim Down said:
I'm not sure exactly which browsers support setSelectionRange,
startSelection and endSelection on textareas, but IE definitely
doesn't. You'll need to look at TextRange for IE - calling
createTextRange() on a textarea will return you a TextRange that
represents all the text in the textarea. You can then move the
boundaries of this TextRange (using move() and similar methods), and
call its select() method to update the user selection.

I should perhaps have mentioned that this is a local application,
intended to reside entirely within one's own machine. Anyway, I'm
developing this (at the moment) for my own purposes and don't have (and
indeed won't have) any interest in IE.

I also verified that createTextRange() is not implemented in Safari4.
 
D

David Mark

I'm not sure exactly which browsers support setSelectionRange,
startSelection and endSelection on textareas, but IE definitely
doesn't. You'll need to look at TextRange for IE - calling
createTextRange() on a textarea will return you a TextRange that
represents all the text in the textarea. You can then move the
boundaries of this TextRange (using move() and similar methods), and
call its select() method to update the user selection.

As with many features, IE has its own proprietary methods. Writing a
cross-browser wrapper is difficult, but not impossible.
 
D

David Mark

I should perhaps have mentioned that this is a local application,
intended to reside entirely within one's own machine. Anyway, I'm
developing this (at the moment) for my own purposes and don't have (and
indeed won't have) any interest in IE.

I also verified that createTextRange() is not implemented in Safari4.

That's one of the IE methods. I wouldn't expect to find it in
Safari. Could happen though, so always check the quasi-standard
methods first.
 
T

Tim Streater

David Mark said:
As with many features, IE has its own proprietary methods. Writing a
cross-browser wrapper is difficult, but not impossible.

I'm not concerned with it being cross-browser (at this point, at any
rate).
 
T

Thomas 'PointedEars' Lahn

Tim said:
I'm aiming to replicate what Mac-Eudora does when you start to type in
an e-mail address into as it might be the To: field. If Eudora
recognises that you seem to be typing in a nickname that it can expand,
it:

1) completes the nickname and highlights the extra characters it has
entered

2) makes that whole nickname <strong>, or bold (but not the rest of the
To: line)

3) allows you to continue typing to complete the nickname or type a
comma to accept it and have it expanded

4) as you type, the highlighted area shrinks one char to the right at a
time


In thinking how I might do this, using onkeypress and onkeyup, I come
across setSelectionRange, startSelection and endSelection for use with a
textarea that I think might be useful in this context (found by some
amount of googling). But none of these is documented as far as I can
tell in for example Danny Goodman's or David Flanagan's JavaScript books.

Maybe because those books are junk, dumped in shelves by wannabes?
(rhetorical question)

That said, the property names are "selectionStart" and "selectionEnd",
respectively. They are well-documented at MDC as they are native to XUL.

<https://developer.mozilla.org/en/XUL/Method/setSelectionRange>
<https://developer.mozilla.org/en/XUL/Property/selectionStart>
These seem to work in Safari 4 beta and FF3 (both Mac), but I'm
concerned they might be deprecated or not be best suited for my needs -
or that they might be part of a useful and well supported set that for
some reason I'm going to have to discover one item at a time.

They are certainly proprietary, but since they have been used, you don't
have to be afraid that they are going away anytime soon. Just be aware of
it and do feature-test them before accessing them (you should do that anyway).


PointedEars
 
D

David Mark

Maybe because those books are junk, dumped in shelves by wannabes?
(rhetorical question)

That said, the property names are "selectionStart" and "selectionEnd",
respectively.  They are well-documented at MDC as they are native to XUL.

<https://developer.mozilla.org/en/XUL/Method/setSelectionRange>
<https://developer.mozilla.org/en/XUL/Property/selectionStart>
<https://developer.mozilla.org/en/XUL/Property/selectionEnd>

But those are XUL.
They are certainly proprietary, but since they have been used, you don't
have to be afraid that they are going away anytime soon.  Just be awareof
it and do feature-test them before accessing them (you should do that anyway).

Why not use the objects I recommended? They work in virtually every
modern browser (except IE of course.) ISTM that XUL objects are a
hard way to go here.
 
R

RobG

In thinking how I might do this, using onkeypress and onkeyup, I come
across setSelectionRange, startSelection and endSelection for use with a
textarea that I think might be useful in this context (found by some
amount of googling). But none of these is documented as far as I can
tell in for example Danny Goodman's or David Flanagan's JavaScript books.

Flanigan covers the subject in section 15.10 Querying Selected Text
(Ed. 5), the methods themselves aren't in the index.

I'm not recommending his coverage as being complete or even correct,
or that you should take his advice over that provided by others here,
I'm just noting that he covers it.
 
T

Tim Streater

Thomas 'PointedEars' Lahn said:
Maybe because those books are junk, dumped in shelves by wannabes?
(rhetorical question)

Is this part of a foolish war about which books are best?

IMO: Goodman's is better laid out and has more detail. But having two
books is better than none. And while a pure reference (ECMA's or anyone
else's) is good, they tend to be rather skeletal.
That said, the property names are "selectionStart" and "selectionEnd",
respectively. They are well-documented at MDC as they are native to XUL.

<https://developer.mozilla.org/en/XUL/Method/setSelectionRange>
<https://developer.mozilla.org/en/XUL/Property/selectionStart>
<https://developer.mozilla.org/en/XUL/Property/selectionEnd>

Yes, I found this. Flanagan makes one reference to XUL which eventually
leads to the URLs above. Useful as a reference but I could find little
discussion.
They are certainly proprietary, but since they have been used, you don't
have to be afraid that they are going away anytime soon. Just be aware of
it and do feature-test them before accessing them (you should do that anyway).

Well, faute de mieux I shall press on with these. Hopefully not as fast
as the Titanic, though.
 
D

David Mark

Is this part of a foolish war about which books are best?

IMO: Goodman's is better laid out and has more detail. But having two
books is better than none. And while a pure reference (ECMA's or anyone
else's) is good, they tend to be rather skeletal.



Yes, I found this. Flanagan makes one reference to XUL which eventually
leads to the URLs above. Useful as a reference but I could find little
discussion.



Well, faute de mieux I shall press on with these. Hopefully not as fast
as the Titanic, though.

Iceberg, right ahead (says "XUL" on it.) Use the objects I suggested.
 
T

Tim Streater

David Mark said:
Iceberg, right ahead (says "XUL" on it.) Use the objects I suggested.

If I can figure out how to relate them to a textarea, then I'll look at
them.
 
T

Thomas 'PointedEars' Lahn

David said:
Iceberg, right ahead (says "XUL" on it.)
Nonsense.

Use the objects I suggested.

Another possibility, not a necessity. And a possibility unlikely to be
equally or more efficient with textareas at that.


PointedEars
 
T

Thomas 'PointedEars' Lahn

Tim said:
Is this part of a foolish war about which books are best?

No, all books published on the subject to date are more or less junk.
That's why there is some debate going on about whether the FAQ should
list any at all at the moment.
IMO: Goodman's is better laid out and has more detail.

Goodman doesn't know what he is talking about, and so does Flanagan.
But having two books is better than none.
Nonsense.

And while a pure reference (ECMA's or anyone else's) is good, they tend
to be rather skeletal.

What does that mean?

Trim your quotes to the relevant minimum, please.

<http://jibbering.com/faq/#posting>


PointedEars
 
T

Thomas 'PointedEars' Lahn

David said:
No, they work with anything.

The universality of Range objects would make them candidates for less
performance than the specialized properties. It is not logical not to use
the latter when they are available.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top