본문 바로가기
부트캠프 개발일지 2023-2024/Bootcamp 생활기록

[7주차] 리액트: 커서 위치 맨 마지막으로 설정하기(setSelectionRange)

by whereanna00 2023. 11. 17.

리액트, 커서 위치 맨 마지막으로 설정하기

  useEffect(() => {
    // focus when it is edit mode
    if (isEditing) {
      const messageLength = messageRef.current.value.length;
      messageRef.current.focus();
      //place the location of the cursor to the last
      messageRef.current.setSelectionRange(messageLength, messageLength);
    }
  }, [isEditing]);

 

 

 

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

 

728x90
반응형