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

javascript - Setting focus to the end of a textarea - Stack Overflow

programmeradmin4浏览0评论
editor.focus();

Sets the focus fine in Chrome. But in Firefox, the focus is set on the beginning on the line. I want it to be set on the end of the line. I tried this:

moveCaretToEnd(ed);

function moveCaretToEnd(el) {
    if (typeof el.selectionStart == "number") {
      el.selectionStart = el.selectionEnd = el.value.length;
    } else if (typeof el.createTextRange != "undefined") {
      el.focus();
      var range = el.createTextRange();
      range.collapse(false);
      range.select();
    }
}

And stupid FF doesn't work again. The focus is gone.

editor.focus();

Sets the focus fine in Chrome. But in Firefox, the focus is set on the beginning on the line. I want it to be set on the end of the line. I tried this:

moveCaretToEnd(ed);

function moveCaretToEnd(el) {
    if (typeof el.selectionStart == "number") {
      el.selectionStart = el.selectionEnd = el.value.length;
    } else if (typeof el.createTextRange != "undefined") {
      el.focus();
      var range = el.createTextRange();
      range.collapse(false);
      range.select();
    }
}

And stupid FF doesn't work again. The focus is gone.

Share Improve this question asked Oct 16, 2012 at 9:10 petko_stankoskipetko_stankoski 10.7k42 gold badges134 silver badges234 bronze badges 1
  • Could you provide jsfiddle demo for this? – Om3ga Commented Oct 16, 2012 at 9:14
Add a ment  | 

1 Answer 1

Reset to default 9

I used the following hack for firefox:

var value = editor.val();
editor.val("");
editor.focus();
editor.val(value);

Here is working fiddle: http://jsfiddle/vyshniakov/p37ax/

发布评论

评论列表(0)

  1. 暂无评论