0
0

【React】テキストボックス1でエンターキーを押してテキストボックス2にフォーカスを変更する

Last updated at Posted at 2024-05-06

タイトル通り、Reactでテキストボックス1でエンターキーを押してテキストボックス2にフォーカスを変更できるよう実装します。

sample.html
<div className="TextBox1">
 テキストボックス1<br />
 <input type="text" id="textbox1" onKeyDown={handleKeyDown} />
</div>
<div className="TextBox2">
 テキストボックス2<br />
 <input type="text" id="textbox2" />
</div>
sample.tsx
const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
  if (e.nativeEvent.isComposing || e.key !== "Enter") return;
  // 次のテキストボックスへ飛ばす処理を記載
  document.getElementById("textbox2")?.focus();
};
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0