0

you can easily find focus() answers from the site or any site.. but what i want to know, how can i use focus after what i just inserted inside the input box.

$("input").val('@user').focus(); // this goes to start of the input box and foucs from start, but i want it to focus after the @user

$("input").val('@user today i did new things').focus(5); // i added 5 in focus method, obviously its not supported yet!, but the idea behind is, to focus it after @user from complete word.

EDITED:

i just saw that on firefox it works fine, but in chrome it starts the cursor from start. any solution?

Basit
  • 16,316
  • 31
  • 93
  • 154

2 Answers2

0

Take a look at this question: jQuery Set Cursor Position in Text Area

Community
  • 1
  • 1
Seb
  • 24,920
  • 5
  • 67
  • 85
0

You can focus an input area by triggering it on change event:

$("input").change(function(){
   $(this).focus();
});

and if you want to check how many characters typed you can make a control on its callback:

//will focus if typed text's length is greater than 5
$("input").change(function(){
   if($(this).val().length > 5){
     $(this).focus();
   }
});

hope it helps, Sinan.

Sinan
  • 11,443
  • 7
  • 37
  • 48
  • it wont focus after the words.. it fouces before the word. i need it to focus after – Basit Oct 28 '09 at 00:41
  • i think you mean setting caret position here not focusing into an input element. Anyway the link in Seb's answer is a good resource to look at. – Sinan Oct 28 '09 at 01:29