Set Focus After Last Character in Text Box

后端 未结 10 2399
梦谈多话
梦谈多话 2020-11-27 13:54

I have 3 text boxes for a phone number. As the user types, it automatically moves from one textbox to the next. When the user presses backspace, I can move focus to the pr

10条回答
  •  遥遥无期
    2020-11-27 14:55

    I tried lots of different solutions, the only one that worked for me was based on the solution by Chris G on this page (but with a slight modification).

    I have turned it into a jQuery plugin for future use for anyone that needs it

    (function($){
        $.fn.setCursorToTextEnd = function() {
            var $initialVal = this.val();
            this.val($initialVal);
        };
    })(jQuery);
    

    example of usage:

    $('#myTextbox').setCursorToTextEnd();
    

提交回复
热议问题