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

I already have this working in Firefox, Safari and Chrome.I would like to be able to programmatically s

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();  

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744045429a4549083.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信