HTMLInputElement: phương thức select()
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Phương thức HTMLInputElement.select() chọn tất cả văn bản trong phần tử <textarea> hoặc trong phần tử <input> bao gồm trường văn bản.
Cú pháp
js
select()
Tham số
Không có.
Giá trị trả về
Không có (undefined).
Ví dụ
Nhấp vào nút trong ví dụ này để chọn tất cả văn bản trong phần tử <input>.
HTML
html
<input type="text" id="text-box" size="20" value="Hello world!" />
<button>Chọn văn bản</button>
JavaScript
js
function selectText() {
const input = document.getElementById("text-box");
input.focus();
input.select();
}
document.querySelector("button").addEventListener("click", selectText);
Kết quả
Ghi chú
Gọi element.select() sẽ không nhất thiết focus đầu vào, vì vậy nó thường được sử dụng cùng với HTMLElement.focus.
Đặc tả kỹ thuật
| Specification |
|---|
| HTML> # dom-textarea/input-select> |