Set keyboard caret position in html textbox

后端 未结 9 2550
时光说笑
时光说笑 2020-11-22 00:02

Does anybody know how to move the keyboard caret in a textbox to a particular position?

For example, if a text-box (e.g. input element, not text-area) has 50 charact

9条回答
  •  别跟我提以往
    2020-11-22 00:28

    I found an easy way to fix this issue, tested in IE and Chrome:

    function setCaret(elemId, caret)
     {
       var elem = document.getElementById(elemId);
       elem.setSelectionRange(caret, caret);
     }
    

    Pass text box id and caret position to this function.

提交回复
热议问题