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,273
  • 10
  • 51
  • 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
  • 151,816
  • 78
  • 307
  • 352