คุณสมบัติ placeholder ของ HTML DOM Input URL ใช้สำหรับดึงหรือกำหนดข้อความแนะนำ (hint) ที่แสดงในช่องป้อน URL เมื่อยังไม่มีค่า
ไวยากรณ์ (Syntax)
การดึงค่า placeholder
inputURLObject.placeholder
การกำหนดค่า placeholder
inputURLObject.placeholder = stringValue
ตัวอย่างการใช้งาน
ตัวอย่างตั้งค่า placeholder ให้ช่อง URL เมื่อคลิกปุ่ม
<!DOCTYPE html>
<html>
<head>
<title>Input URL placeholder</title>
<style>
form {
width: 70%;
margin: 0 auto;
text-align: center;
}
* {
padding: 2px;
margin: 5px;
}
input[type="button"] {
border-radius: 10px;
}
</style>
</head>
<body>
<form>
<fieldset>
<legend>URL-placeholder</legend>
<label for="URLSelect">URL: <input type="url" id="URLSelect"> </label>
<input type="button" value="Get an Example" onclick="setPlaceholder()">
</fieldset>
</form>
<script>
var inputURL = document.getElementById("URLSelect");
function setPlaceholder() {
inputURL.placeholder = 'https://www.google.com';
}
</script>
</body>
</html>
ผลลัพธ์
ก่อนคลิกปุ่ม - ช่อง URL ว่าง ไม่มี placeholder
หลังคลิก "Get an Example" - placeholder แสดง "https://www.google.com" (สีจาง หายเมื่อพิมพ์)
แนวทางปฏิบัติที่ดี: ใช้ placeholder สำหรับตัวอย่างรูปแบบ ไม่ใช่แทน label (เพื่อ accessibility)