Solved

How to use textRange Object to move the cursor?

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
email validation 9 47
how can i break an array into a couple 5 22
Table doesn't show the lines! 3 23
Two decimal 5 25
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

680 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