Joho the Blog
An Entry from the Archives

« Collaborative band name exchange || Back to Blog | Webby Sunday Funnie »

September 08, 2007

Beginner to Beginner: Setting the cursor in a textarea via Javascript, in Firefox

Suppose you want to positing the insertion point in a textarea on an HTML page using javascript. (If you don't want to suppose that, then stop reading now.) This is basic stuff for real programmers, but it took me longer than it should to figure out exactly how to get this to work, so pardon my step by step instructions. I figure it might save some other clodhopping amateur like me the effort of figuring out what's so obvious to real programmers that they don't bother mentioning it.

So, let's say your HTML page has a textarea element — you know, one of them type-in fields. In fact, let's say the entire body of your page looks like this at the code level:

<textarea id="ta">01234567</textarea>
<input type="button" value="Move cursor to after first letter" onclick="setsel()">

That'll make a page that looks like this:

(The button doesn't work. It's just for show. But see below.)

In case any of this is unclear, the first line creates a textarea with an identifer that I made up ("ta"), and containing "01234567" as its initial text. The second line creates the button, gives it a label, and says that when a user clicks it, the function "setsel()" should be executed. But you already knew that.

Here's the operative part of the function "setsel()":

var tarea=document.getElementById("ta");
tarea.focus();
tarea.setSelectionRange(1,1);

The first line creates a variable called "tarea" and assigns it the textarea that has the identifier "ta."

The second line tells it that that's the element on the page that is going to receive the user interaction. (I lost a couple of hours not realizing the textarea had lost the focus. You may not always need this line. But what could it hurt?)

The third contains the command that works in Firefox (but not IE) for selecting a range of text. It takes two parameters: The position of the character where the selection should start and the position of the character where it should end. (Note: The second value does not say how many characters should be selected.) This is a zero-based system: The spot before the first character is 0. So, if you tell setSelectionRange (and capitalization counts!) to begin at 0 and end at 1, it will select the first character. If you tell it to begin and end at, say, 1, nothing will be selected, and the insertion point will be between the "0" and the "1" in this particular example.

Because I am such a fun guy, fill in the values you want in the following two boxes and click away to select the text in the same textarea. (Remember, only Firefoxers can play this exciting game!)

You'd better read any comments before taking this seriously because I'm guaranteed to have gotten it wrong, possibly in destructive and certainly in embarrassing ways. [Tags: javascript amateurs setSelectionRange mozilla firefox tutorial wrong_in_public_again]

Posted by D. Weinberger at September 8, 2007 08:07 PM


Comments

That was fun, and it works fine here in Firefox on the Mac. :-)

Posted by: Dave Winer | September 8, 2007 10:05 PM


And on Camino, an FF-based Mac-specific browser.

Posted by: Britt Blaser | September 8, 2007 11:19 PM


Opera buffs(?) can play too!

Did a quick Google for the IE-equivalent incantation, thinking perhaps a simple bit of code mediation could let everyone play. First glance looks unlikely, a maze of twisty little code forks, all alike.

Second glance looks a little better. This article about implementing autosuggest in a textarea shows how to select a range for both IE / Mozilla. (Use the selectRange() function substituting your textarea for 'this.textbox'.)

Then again, better not. Testing the above suggestion caused IE to terminate and ask to phone home (IE 6 on WinXP and IE5.5 on Win98). Apparently there is a very serious security flaw in createTextRange() and we should not be attempting this at home. Twisty.

Posted by: Wray Cummings | September 9, 2007 12:29 PM


I'm reading this in OmniWeb for the Mac and can report that this trick works there too.

Posted by: Karig | October 15, 2007 06:04 AM


Post a comment

Guidelines for Commenting

Basically, you can say what you want. (Click here for the fine print.)

If you haven't left a comment here before, your comment may be put into a queue for me to approve. Sorry for the delay. Blame the damn spammers.