最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I set the cursor to a particular position in the string value of a text INPUT field in Internet Explorer? -

programmeradmin2浏览0评论

I already have this working in Firefox, Safari and Chrome.

I would like to be able to programmatically set the position of the text cursor within an INPUT field in Internet Explorer.

I looked this topic up on various websites and generally found the same technique:

var el = document.getElementById("myTextField");
var pos = 6;

if (document.selection) {
    el.focus();
    var selection = document.selection.createRange();
    selection.moveStart("character", -el.value.length);
    selection.moveStart("character", pos);
    selection.moveEnd("character", 0);
    selection.select();
}

The problem is that when I try to do this the cursor always goes to the end of the value regardless of what position I provide.

Did I misunderstand the technique people have been using? Did I miss something somewhere? It's a little bit frustrating, but of course that's the nature of web development with these different browsers.

Thanks in advance for any help.

I already have this working in Firefox, Safari and Chrome.

I would like to be able to programmatically set the position of the text cursor within an INPUT field in Internet Explorer.

I looked this topic up on various websites and generally found the same technique:

var el = document.getElementById("myTextField");
var pos = 6;

if (document.selection) {
    el.focus();
    var selection = document.selection.createRange();
    selection.moveStart("character", -el.value.length);
    selection.moveStart("character", pos);
    selection.moveEnd("character", 0);
    selection.select();
}

The problem is that when I try to do this the cursor always goes to the end of the value regardless of what position I provide.

Did I misunderstand the technique people have been using? Did I miss something somewhere? It's a little bit frustrating, but of course that's the nature of web development with these different browsers.

Thanks in advance for any help.

Share Improve this question edited Jan 19, 2022 at 20:27 General Grievance 5,04338 gold badges37 silver badges56 bronze badges asked Jul 25, 2011 at 21:13 natlee75natlee75 5,1973 gold badges36 silver badges39 bronze badges 3
  • which version of IE are you testing in, from what I've found this script is for IE6 and hasn't been tested in later versions. – Greg Guida Commented Jul 25, 2011 at 21:25
  • What versions of IE do you need to support? Looks like they are pickier about acceptable formats. See Quirks patibility chart - quirksmode/dom/range_intro.html – mrtsherman Commented Jul 25, 2011 at 21:29
  • I have been testing in Internet Explorer 7 & 8. Internet Explorer 9 seems to work fine with the code that I use to handle Firefox. – natlee75 Commented Jul 26, 2011 at 2:39
Add a ment  | 

1 Answer 1

Reset to default 7

The following code is working for me in IE 9

<script type="text/javascript">
    var input = document.getElementById("myInput");
    input.selectionStart = 2;
    input.selectionEnd = 5;
</script>

Here is the code that I'm using for IE 6

      input.select();
        var sel = document.selection.createRange();
        sel.collapse();
        sel.moveStart('character', this.SelectionStart);
        sel.collapse();
        sel.moveEnd('character', this.SelectionEnd - this.SelectionStart);
        sel.select();  
发布评论

评论列表(0)

  1. 暂无评论