JavaScript / jQueryにてページ表示時に指定したテキストボックスにフォーカスさせる方法
ページを開いた際に指定したテキストボックス(input type="text")にフォーカスさせてほしいという要望を受けた。JavaScript及びjQueryでの対応方法をメモ。
対応方法
ソースコード
<input type="text" id="test"> <script type="text/javascript" src="https://code.jquery.com/jquery-3.7.1.min.js"></script> <script> //JavaScriptの場合 document.querySelector(`#test`).focus(); $(function(){ //jQueryの場合 $(`#test`).focus(); }); </script>
リファレンス
HTMLElement: focus() メソッド
https://developer.mozilla.org/ja/docs/Web/API/HTMLElement/focus
.focus()
関連記事
-
-
FormDataを使ったAjax通信がiPhoneのみエラー
FormDataオブジェクトを使用したAjax通信するシステムがあり、PCやAn ...
-
-
jQueryで画像(
タグ内)のsrc情報を取得する場合
すぐに出てこなかったのでメモ。 $('#test').attr('src'); ...
-
-
AjaxFileUploadで処理は実行できてもエラーが返る
AjaxFileUploadでPHPに通信し、サーバー側のPHPで処理は正常に実 ...
-
-
PHPとajaxでチャットの作成
■ソース ・index.php <? error_reporting(E_ ...
-
-
jQuery Nice Selectを特定ページのみ無効にする方法
bootstrap系のテンプレートデザインを使用したサイトを構築中に、セレクトメ ...