คุณสมบัติ type ของ HTML DOM Input DatetimeLocal ใช้สำหรับดึงหรือกำหนดชนิดของช่องกรอกข้อมูลสำหรับ datetime-local
ไวยากรณ์ (Syntax)
การดึงค่า (คืนค่า string)
inputDatetimeLocalObject.type
การกำหนดค่า type
inputDatetimeLocalObject.type = stringValue
ค่า String ที่รองรับ (String Values)
| stringValue | รายละเอียด |
|---|---|
| date | กำหนดให้ input type เป็น date |
| datetime-local | กำหนดให้ input type เป็น datetime-local |
| checkbox | กำหนดให้ input type เป็น checkbox |
| text | กำหนดให้ input type เป็น text |
ตัวอย่างการใช้งาน
ตัวอย่างด้านล่างตรวจสอบ type ของช่อง datetime-local เพื่อระบุสถานที่จัดงาน:
<!DOCTYPE html>
<html>
<head>
<title>Input DatetimeLocal type</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>Datetime-Local-type</legend>
<label for="datetimeLocalSelect">Inauguration Date-Time : <input type="datetime-local" id="datetimeLocalSelect" value="2020-01-01T10:00"> </label>
<input type="button" onclick="getEvent()" value="Where is the event? ">
<div id="divDisplay"></div>
</fieldset>
</form>
<script>
var divDisplay = document.getElementById("divDisplay");
var inputDatetimeLocal = document.getElementById("datetimeLocalSelect");
function getEvent() {
if(inputDatetimeLocal.type === 'datetime-local')
divDisplay.textContent = 'Event Inauguration near you';
else
divDisplay.textContent = 'Event Inauguration in India';
}
</script>
</body>
</html>
ผลลัพธ์ (Output)
ก่อนคลิกปุ่ม: ไม่แสดงข้อความ หลังคลิกปุ่ม Where is the event?: หาก type เป็น datetime-local จะแสดง "Event Inauguration near you" มิฉะนั้นจะแสดง "Event Inauguration in India"