Solved

How to use textRange Object to move the cursor?

Posted on 2001-08-09
5
280 Views
Last Modified: 2010-08-05
Using IE 5.5, when the user gives focus to an empty text or textArea, I automatically place a default value in the box like so:

function myOnFocus ()
   {
   var o = event.srcElement;
   if ("" == myTrim(o.value))
      {
      o.value = "default value";
      // Location A
      }
   }

The problem is that the text-insertion cursor appears at the beginning of this text and I would like it to appear at the end.

I can select the text using the following code at Location A:

      var r = o.createTextRange();
      r.select ();

Documentation suggests that the following code would then remove the selection and move the cursor to the end, but it does nothing that I can detect:

      r.collapse (false);

Can anyone suggest how I might move the cursor to the end of the text using the TextRange object or by some other means?

Thank you all :)

Musashi
0
Comment
Question by:Musashi
5 Comments
 
LVL 1

Expert Comment

by:Y_ee
ID: 6370058
post your question in this mail list is about DOM browser
they'll love to help you
http://groups.yahoo.com/group/wdf-dom

ciao
Y
0
 
LVL 3

Expert Comment

by:nzjonboy
ID: 6370451
Musashi

try putting a focus statement in before you end your if statement, like so

function myOnFocus ()
  {
  var o = event.srcElement;
  if ("" == myTrim(o.value))
     {
     o.value = "default value";
     o.focus();

     // Location A
     }
  }

hope this helps

nzjonboy
0
 
LVL 1

Author Comment

by:Musashi
ID: 6380157
nzjonboy, I am not sure why that works for you (especially since the object obviously already has the focus), but it does not for me.  I see the text appear with the cursor at the end of the text, then after a short pause, it jumps to (and stays at) the beginning of the text.  I am currently testing it with a text box, perhaps a textArea behaves differently.

Y_ee thanks for the tip, I will try that avenue also, although at first glance, it looks like they are more standards oriented than help with questions oriented.

Musashi :)
0
 
LVL 1

Accepted Solution

by:
lpkuah earned 75 total points
ID: 6748031
Hi,

To move the cursor to the last position, you can do this :

var t = document.frm.textbox.createTextRange();
t.move ("character", document.frm.textbox.value.length)
t.select();
0
 
LVL 1

Author Comment

by:Musashi
ID: 6749418
Wow, I am impressed that you even found my question after all this time.  Such a simple answer too.  I am sure that I tried this, but without the select(), because it didn't seem like the select should be needed. oh well.

Thanks much!
Musashi

P.S. I bumped the points up in gratitude :)
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now