ใช่ สามารถเลือก Text Box ได้ด้วยเมธอด select()
ตัวอย่างโค้ด
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
Enter your Name: <input type="text" id="txtName" value="John"> <br>
<button type="button" onclick="check()">Select Text Box</button>
<script>
function check() {
document.getElementById("txtName").select();
}
</script>
</body>
</html>
วิธีรันโปรแกรม
- บันทึกไฟล์ชื่อ "anyName.html" หรือ "index.html"
- คลิกขวาที่ไฟล์ เลือก "Open with Live Server" ใน VS Code Editor
ผลลัพธ์
คลิกปุ่ม "Select Text Box" จะเลือกข้อความในช่อง Input อัตโนมัติ