คุณสมบัติชื่อช่องกาเครื่องหมายอินพุต HTML DOM ส่งกลับสตริง ซึ่งเป็นค่าของแอตทริบิวต์ชื่อของช่องกาเครื่องหมายอินพุต ผู้ใช้ยังสามารถตั้งค่าเป็นสตริงใหม่ได้
ไวยากรณ์
ต่อไปนี้เป็นไวยากรณ์ -
- คืนค่าสตริง
inputCheckboxObject.name
- ตั้งค่า ชื่อ แอตทริบิวต์เป็นค่าสตริง
inputCheckboxObject.name = ‘String’
ตัวอย่าง
ให้เราดูตัวอย่างคุณสมบัติชื่อช่องทำเครื่องหมายอินพุต -
<!DOCTYPE html>
<html>
<head>
<title>Name Attribute of Checkbox</title>
</head>
<body>
<form id="Form">
<div>
Enable new name: <input id="formID" type="checkbox" name="formID">
</div>
</form>
<button onclick="getFormID()">Change Name Attribute</button>
<div id="nameAttr"></div>
<script>
function getFormID(){
var oldNameAttr = document.getElementById("formID");
var newNameAttr = document.getElementById("nameAttr");
if(oldNameAttr.checked == true){
oldNameAttr.name = 'newFormID';
newNameAttr.textContent = 'Updated value of name attribute: '+oldNameAttr.name;
} else {
newNameAttr.textContent = 'Check the checkbox'+ ', current value of name attribute: '+oldNameAttr.name ;
}
}
</script>
</body>
</html> ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
ก่อนตรวจสอบ 'เปิดใช้งานชื่อใหม่' ช่องทำเครื่องหมาย -

หลังจากทำเครื่องหมายที่ 'เปิดใช้งานชื่อใหม่' ช่องทำเครื่องหมาย -
