开发者

jQuery: How to focus on input text field such that the cursor is placed at the end of the text?

开发者 https://www.devze.com 2023-04-07 05:47 出处:网络
Consider the following example: (demo here) HTML: <input type=\"text\" /> <div>Click here to set focus</div>

Consider the following example: (demo here)

HTML:

<input type="text" />
<div>Click here to set focus</div>

JS:

$(function开发者_StackOverflow中文版() {
    $("input").val("Hello");
    $("div").click(function() {
        $("input").focus();
    });
});

When the div is clicked, the cursor positioned at the beginning of the text: |Hello.

How could I set the cursor at the end of the text: Hello| ?


Use this plugin: http://plugins.jquery.com/project/PutCursorAtEnd


I think you are looking for selectionStart.

0

精彩评论

暂无评论...
验证码 换一张
取 消