คุณสมบัติ name ของ HTML DOM Input Checkbox คืนค่าสตริงซึ่งเป็นค่าของแอตทริบิวต์ name ของ input checkbox ผู้ใช้สามารถตั้งค่าเป็นสตริงใหม่ได้
รูปแบบไวยากรณ์ (Syntax)
- การคืนค่าสตริง
inputCheckboxObject.name
- การตั้งค่าแอตทริบิวต์ name เป็นค่าสตริง
inputCheckboxObject.name = ‘String’
ตัวอย่าง
ตัวอย่างการใช้งานคุณสมบัติ Input Checkbox name:
<!DOCTYPE html> <html> <head> <title>แอตทริบิวต์ Name ของ Checkbox</title> </head> <body> <form id="Form"> <div> เปิดใช้ชื่อใหม่: <input id="formID" type="checkbox" name="formID"> </div> </form> <button onclick="getFormID()">เปลี่ยนแอตทริบิวต์ Name</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 = 'ค่า name attribute ที่อัปเดต: '+oldNameAttr.name; } else { newNameAttr.textContent = 'ติ๊กที่ checkbox'+ ', ค่า name attribute ปัจจุบัน: '+oldNameAttr.name ; } } </script> </body> </html>
ผลลัพธ์
โค้ดด้านบนจะให้ผลลัพธ์ดังนี้:
ก่อนติ๊ก checkbox 「เปิดใช้ชื่อใหม่」 :

หลังติ๊ก checkbox 「เปิดใช้ชื่อใหม่」 :
