Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting focus to the end of a textarea

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.

like image 882
petko_stankoski Avatar asked Jan 15 '23 07:01

petko_stankoski


1 Answers

I used the following hack for firefox:

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

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

like image 66
Artem Vyshniakov Avatar answered Jan 26 '23 01:01

Artem Vyshniakov