Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to use textRange Object to move the cursor?

Posted on 2001-08-09
5
Medium Priority
?
300 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 300 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…

704 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