jquery focus, but with more accurate postion
-
06-07-2019 - |
Question
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?
Solution
Take a look at this question: jQuery Set Cursor Position in Text Area
OTHER TIPS
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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow