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 Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with cursor position in textbox when it gets focus

Status
Not open for further replies.

newora

Programmer
Aug 19, 2003
133
GB
Hi There,

Apologies is this is another stupid newbie query, but I have been searching the web for about an hour and can not find anything to help me out - did find lots of interesting ASP .NET stuff though, so not a waste of time!!

I have a textbox on a webform that has some text placed into it on the page_load event. There is also a button on the form and when I click the button I want to set the focus to the textbox which has the required text already shown on the page.

I do this using a Javascript function :-document.getElementById('control ID').focus()

This works fine but when the control receives the focus, the cursor is placed at the start (i.e. left hand side) of the text that is already in the textbox and I would really like it to be at the end (i.e. right hand side of the text.

Am I misisng something completely obvious? - I suppose I am looking for something similiar to the VB .NET sendkeys function. Thinking about it I suppose that the Javascript function does not know anything about the text that is in the control!

Thanks for your assistance.

 
I think you will need to use JavaScript to postion the cursor where you want it. You may want to check in the javascript forum.
 
Great thanks - just found a javascript function to do it. Just got to get it working now !!

Thanks again
 
Finally got there (I hope). The following code is used to set the focus to a textbox and then place the cursor at the end of any current contents :-

Private Sub setfocus(ByVal ctrl As Web.UI.Control)
Page.RegisterStartupScript("SetFocus", _
"<script>document.getElementById('" & ctrl.ClientID & _
"').focus();" _
& "document.getElementById('" & ctrl.ClientID & "').value = " & "document.getElementById('" & ctrl.ClientID & "').value" _
& "</script>")
End Sub

In the end and after a lot of interesting reading on Javascript, to set the cursor, you just set the value property of the textbox control equal to itself. Only tested on IE 7 though.
 
humm that is interesting..I'll have to play with that.
Thanks for posting..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top