개념
<input /> <textarea /> element에서 특정 문자열을 세팅 (하이라이팅)
See the Pen setSelectionRange by Daewoong Kim (@daewoong-kim) on CodePen.
시행착오 1
focus 이벤트를 받고, 그 안에서 setSelectionRange를 선언 후 scrollLeft를 해주었더니,
브라우저 기본 동작인 input 클릭 시 해당 위치로 커서를 이동시키는 동작 보다 addEventListener를 통해 등록된 focus 이벤트 콜백 함수가 먼저 실행되는 타이밍 이슈가 발생
(마우스를 이용하지 않고 키보드로 포커스를 주면 이상 없음)
시행착오 2
mouseup 이벤트 사용해보았지만 focus일 때 동작하지 않음.
결론
포커스 되면 setTimeout 사용하여 커서를 이동시킴
See the Pen Untitled by Daewoong Kim (@daewoong-kim) on CodePen.
https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/setSelectionRange
HTMLInputElement: setSelectionRange() method - Web APIs | MDN
The HTMLInputElement.setSelectionRange() method sets the start and end positions of the current text selection in an <input> or <textarea> element.
developer.mozilla.org
https://fe-developers.kakaoent.com/2021/211104-setselectionrange/
setSelectionRange로 검색창 커서 옮기기 | 카카오엔터테인먼트 FE 기술블로그
이윤용(ibra) 곧 10년차 개발자입니다. 세 아이의 아빠이고, 축구를 좋아합니다. 현재 카카오웹툰 프론트엔드 개발을 담당하고 있습니다.
fe-developers.kakaoent.com
'IT' 카테고리의 다른 글
[Github Action] 개념 (0) | 2024.02.13 |
---|---|
[Web] 다크모드 지원하기 (0) | 2024.02.08 |
oh my zsh로 업무 생산성 높히기 (1) | 2024.01.21 |
CSS 우선순위 (0) | 2024.01.09 |
firebase web hosting 배포 시 node.js 버전 해결 방법 (0) | 2023.12.17 |