Solved

How to use textRange Object to move the cursor?

Posted on 2001-08-09
5
283 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

920 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

17 Experts available now in Live!

Get 1:1 Help Now