Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
30
rated 0 times [  37] [ 7]  / answers: 1 / hits: 111939  / 14 Years ago, tue, february 8, 2011, 12:00:00

How do I get the index of the text caret in an input?


More From » html

 Answers
28

-> selectionStart


<!doctype html>

<html>
<head>
<meta charset = "utf-8">

<script type = "text/javascript">
window.addEventListener ("load", function () {
var input = document.getElementsByTagName ("input");

input[0].addEventListener ("keydown", function () {
alert ("Caret position: " + this.selectionStart);

// You can also set the caret: this.selectionStart = 2;
});
});
</script>

<title>Test</title>
</head>

<body>
<input type = "text">
</body>
</html>

[#93848] Sunday, February 6, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jarrodfletchers

Total Points: 75
Total Questions: 94
Total Answers: 95

Location: Netherlands
Member since Thu, Jul 1, 2021
3 Years ago
jarrodfletchers questions
Sat, Sep 25, 21, 00:00, 3 Years ago
Mon, Jan 21, 19, 00:00, 5 Years ago
Sun, Dec 16, 18, 00:00, 6 Years ago
Sun, Nov 4, 18, 00:00, 6 Years ago
;