Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

onLoad move to START of text field

Status
Not open for further replies.

Microbe

Programmer
Joined
Oct 16, 2000
Messages
607
Location
AU
Hey folks,

I have a <body onLoad()) that pops the cursor into a text field.

The minor annoyance is that because there is existing text it drops the cursor at the end of the field.

Is there anyway to make it fake a home key press so the cursor is positions at the left of the text?

Thanks in advance.

Steve Davis

NOTE: This sig does not include any reference to voting, stars, or marking posts as helpful as doing so is cause for membership termination.
 
This works in IE
Code:
var input = document.getElementById('myfield');
var range = input.createTextRange();
  range.collapse(true);
  range.moveEnd('character', 0);
  range.moveStart('character', 0);
  range.select();

First you get the object then you create a text range for the object.
range.collapse will clear any previous settings for the range and the moveEnd and moveStart will set the positions that the text range will cover. If you set it at start of 0 and end of 5 then the first 5 characters will be highlighted but by setting both numbers to 0 the cursor position is at 0 and nothing is highlighted.

I do not know if the same holds true for other browsers.


At my age I still learn something new every day, but I forget two others.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top