admin管理员组文章数量:1022761
I am trying to get the position of the text cursor immediately after one clicks down the mouse to reposition it. The issue is that selectionStart
and selectionEnd
do not return the most current position of the cursor, since the "new" position doesn't get stored until the click is released. The code snippet shows this problem when you try to reposition the caret with the mouse.
This is strange because I can technically type at the "new" position as soon as the mouse is clicked down, but selectionStart
still returns the old position. If the cursor position can clearly change without having to release the click, then how do you access the new position without having to wait for the mouseup
?
(this also seems to be a problem for tracking cursor position after keydowns)
const input = document.getElementById('myInput');
input.addEventListener('mousedown', showposition); // click
function showposition() {
document.getElementById("output").innerHTML += " " + input.selectionStart;
}
<input id="myInput">
<p id="output"></p>
I am trying to get the position of the text cursor immediately after one clicks down the mouse to reposition it. The issue is that selectionStart
and selectionEnd
do not return the most current position of the cursor, since the "new" position doesn't get stored until the click is released. The code snippet shows this problem when you try to reposition the caret with the mouse.
This is strange because I can technically type at the "new" position as soon as the mouse is clicked down, but selectionStart
still returns the old position. If the cursor position can clearly change without having to release the click, then how do you access the new position without having to wait for the mouseup
?
(this also seems to be a problem for tracking cursor position after keydowns)
const input = document.getElementById('myInput');
input.addEventListener('mousedown', showposition); // click
function showposition() {
document.getElementById("output").innerHTML += " " + input.selectionStart;
}
<input id="myInput">
<p id="output"></p>
Share
Improve this question
asked Jun 10, 2020 at 18:05
JakeJake
1192 silver badges7 bronze badges
1 Answer
Reset to default 3Use the click
event instead of the mousedown
event. This is because, at the point when the mousedown event is triggered, the selectionStart
would not have been updated which is why you keep getting the previous values.
Click event on the other hand is a bination of mouseup
+ mousedown
event which captures one plete mouse click and returns the expected cursor position value.
const input = document.getElementById('myInput');
input.addEventListener('click', showposition); // click
function showposition(event) {
document.getElementById("output").innerHTML += " " + event.target.selectionStart;
}
<input id="myInput">
<p id="output"></p>
I am trying to get the position of the text cursor immediately after one clicks down the mouse to reposition it. The issue is that selectionStart
and selectionEnd
do not return the most current position of the cursor, since the "new" position doesn't get stored until the click is released. The code snippet shows this problem when you try to reposition the caret with the mouse.
This is strange because I can technically type at the "new" position as soon as the mouse is clicked down, but selectionStart
still returns the old position. If the cursor position can clearly change without having to release the click, then how do you access the new position without having to wait for the mouseup
?
(this also seems to be a problem for tracking cursor position after keydowns)
const input = document.getElementById('myInput');
input.addEventListener('mousedown', showposition); // click
function showposition() {
document.getElementById("output").innerHTML += " " + input.selectionStart;
}
<input id="myInput">
<p id="output"></p>
I am trying to get the position of the text cursor immediately after one clicks down the mouse to reposition it. The issue is that selectionStart
and selectionEnd
do not return the most current position of the cursor, since the "new" position doesn't get stored until the click is released. The code snippet shows this problem when you try to reposition the caret with the mouse.
This is strange because I can technically type at the "new" position as soon as the mouse is clicked down, but selectionStart
still returns the old position. If the cursor position can clearly change without having to release the click, then how do you access the new position without having to wait for the mouseup
?
(this also seems to be a problem for tracking cursor position after keydowns)
const input = document.getElementById('myInput');
input.addEventListener('mousedown', showposition); // click
function showposition() {
document.getElementById("output").innerHTML += " " + input.selectionStart;
}
<input id="myInput">
<p id="output"></p>
Share
Improve this question
asked Jun 10, 2020 at 18:05
JakeJake
1192 silver badges7 bronze badges
1 Answer
Reset to default 3Use the click
event instead of the mousedown
event. This is because, at the point when the mousedown event is triggered, the selectionStart
would not have been updated which is why you keep getting the previous values.
Click event on the other hand is a bination of mouseup
+ mousedown
event which captures one plete mouse click and returns the expected cursor position value.
const input = document.getElementById('myInput');
input.addEventListener('click', showposition); // click
function showposition(event) {
document.getElementById("output").innerHTML += " " + event.target.selectionStart;
}
<input id="myInput">
<p id="output"></p>
本文标签:
版权声明:本文标题:javascript - How do you get the current cursor position (in a textarea) whenever the cursor or arrow key is pressed DOWN? - Stac 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745574868a2156955.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论