3

Possible Duplicate:
Set Focus After Last Character in Text Box

I have a text field that has a default value in it. I would like to use jquery to focus on this field, but using focus() puts the cursor before the text that's already entered. How can I focus the cursor after the entered text?

Community
  • 1
  • 1
James
  • 5,163
  • 10
  • 50
  • 76

1 Answers1

1

You can set the caret position like this: http://jsfiddle.net/tSChT/.

var textbox = $("#textbox");

textbox.prop("selectionStart", textbox.val().length) // set caret to length (end)
       .focus();
pimvdb
  • 146,912
  • 75
  • 297
  • 349