[javascript] ie focus 문제 및 커서 맨뒤로 보내기

ie 에서 focus() 호출시 커서가 맨 앞에 위치하는 문제 해결

develop, javascript
written byzuhern1zuhern

in

2019. 03. 20


ie 에서 draggable 안에 있는 text input cursor가 한번에 잡히지 않아서 쓴 코드

function onClickInput(event) {
  
  var target = event.currentTarget;

  $(target).blur();
  $(target).focus();
  
  if (vm.isIE) {
    moveCursorToEnd(target);
  }
}

function moveCursorToEnd(el) {
  $timeout(function () {
    if (typeof el.selectionStart == "number") {
      el.selectionStart = el.selectionEnd = el.value.length;
    } else if (typeof el.createTextRange != "undefined") {
      var range = el.createTextRange();
      range.collapse(false);
      range.select();
    }
  }, 1);
}

ie 에서 focus 대신 select 을 써보자

focus() 하면 커서가 맨 앞으로 가고
select() 하면 block 선택이 된다. 차라리 이게 낫지 않은가?!ㅋㅋ

더 좋은 해결 책

수정 모드일 때는 draggable 을 제거한다.
draggable=false 여도 작동한다는 점을 유의할 것

$element[0].removeAttribute('draggable');