admin管理员组文章数量:1334133
I have this text box:
<input type="text" name="url" id="url-input" />
and this code:
var inputText = "Hello World";
$(document).ready(function() {
$("#url-input").focus();
$("#url-input").val(inputText);
});
As it is, the cursor is displayed at the end of the textbox after the words "Hello World" (no matter in which order I add focus or change the value). How can I move it to the front?
Thanks
EDIT - Here's the jsFiddle: /
I have this text box:
<input type="text" name="url" id="url-input" />
and this code:
var inputText = "Hello World";
$(document).ready(function() {
$("#url-input").focus();
$("#url-input").val(inputText);
});
As it is, the cursor is displayed at the end of the textbox after the words "Hello World" (no matter in which order I add focus or change the value). How can I move it to the front?
Thanks
EDIT - Here's the jsFiddle: http://jsfiddle/dmRND/
Share Improve this question edited Jul 13, 2011 at 17:34 Nick Brunt asked Jul 13, 2011 at 17:28 Nick BruntNick Brunt 10.1k11 gold badges57 silver badges84 bronze badges2 Answers
Reset to default 7In decent web browsers, you have access to the selectionStart
and selectionEnd
properties which represent the selection. When they are both 0
, it es down to no selection and the caret at the beginning:
$("#url-input").prop('selectionStart', 0)
.prop('selectionEnd', 0);
http://jsfiddle/efek3/
Another thing of jQuery is chaining: $(...).func1().func2()
, so you need the selector once only.
Yet another thing of jQuery is that most functions support objects as parameter so you could also do:
$("#url-input").prop({ selectionStart: 0,
selectionEnd: 0 });
I use this small jquery plug in: https://gist.github./DrPheltRight/1007907
本文标签: javascriptHow do I move the cursor to the front of a textbox which has text in itStack Overflow
版权声明:本文标题:javascript - How do I move the cursor to the front of a textbox which has text in it? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742358677a2459924.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论