テキストボックス(またはテキストエリア)を全選択

テキストボックス(またはテキストエリア)を全選択するには、element.selectメソッドを使用します。

構文

JavaScript

element.select();

デモ

テキストボックス:

テキストエリア:

サンプルコード

JavaScript (ES5)

var inputElem = document.getElementById('text');

if (inputElem) {
	inputElem.onmouseover = function() {
		this.select();
	};
}

JavaScript (ES6以降)

const inputElem = document.querySelector('#text');

if (inputElem) {
	inputElem.addEventListener('mouseover', function() {
		inputElem.select();
	});
}

JavaScript逆引きリファレンス一覧へ戻る