กล่องกาเครื่องหมายอินพุต HTML DOM ที่ตรวจสอบคุณสมบัติจะส่งกลับและแก้ไขค่าของแอตทริบิวต์ที่เลือกของช่องทำเครื่องหมายใน HTML
ไวยากรณ์
ต่อไปนี้เป็นไวยากรณ์ -
1. ตรวจสอบการส่งคืน
object.checked
2. ตรวจสอบการเปลี่ยนแปลง
object.checked = true|false
ตัวอย่าง
ให้เราดูตัวอย่างของ HTML DOM Input Checkbox ที่ตรวจสอบคุณสมบัติ -
<!DOCTYPE html>
<html>
<head>
<title>HTML DOM checked property</title>
<style>
body{
text-align:center;
}
p{
font-size:1.5rem;
color:#ff8741;
}
input{
width:30px;
height:30px;
}
button{
background-color:#db133a;
color:#fff;
padding:8px;
border:none;
width:120px;
margin:0.5rem;
border-radius:50px;
outline:none;
}
</style>
</head>
<body>
<h1>checked Property Example</h1>
<p>Are you happy?</p>
<input type="checkbox">
<br>
<button onclick="yes()">Yes</button>
<button onclick="no()">No</button>
<script>
function yes() {
document.querySelector("input").checked = true;
}
function no() {
document.querySelector("input").checked = false;
}
</script>
</body>
</html> ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -

คลิกที่ปุ่ม "ใช่" เพื่อทำเครื่องหมายที่ช่อง

ตอนนี้ คลิกที่ “ไม่ ” เพื่อยกเลิกการทำเครื่องหมายที่ช่อง
