Solved

How to use textRange Object to move the cursor?

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What is the correct way to check for empty value in input field 2 35
Boolean 13 50
Make Float not to Wrap 15 66
Change javascript filter code from OR logic to AND 5 41
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 …
This article discusses how to create an extensible mechanism for linked drop downs.
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…

738 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