Misha Moroshko
Misha Moroshko

Reputation: 171321

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

Consider the following example: (demo here)

HTML:

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

JS:

$(function() {
    $("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| ?

Upvotes: 3

Views: 1053

Answers (2)

PiTheNumber
PiTheNumber

Reputation: 23542

I think you are looking for selectionStart.

Upvotes: 0

Billy Moon
Billy Moon

Reputation: 58521

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

Upvotes: 1

Related Questions