คุณสมบัติ step ของ HTML DOM Input Number ใช้สำหรับคืนค่าและเปลี่ยนแปลงค่าของแอตทริบิวต์ step ของช่องป้อนตัวเลขในเอกสาร HTML
รูปแบบไวยากรณ์ (Syntax)
การคืนค่า step
object.step
การเปลี่ยนแปลง step
object.step = “number”
ตัวอย่าง
ตัวอย่างการใช้งานคุณสมบัติ step ของ HTML DOM Input Number:
<!DOCTYPE html> <html> <head> <style> html{ height:100%; } body{ text-align:center; color:#fff; background: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%) center/cover no-repeat; height:100%; } p{ font-weight:700; font-size:1.1rem; } input{ display:block; width:35%; border:2px solid #fff; background-color:transparent; color:#fff; font-weight:bold; padding:8px; margin:1rem auto; } .btn{ background:#0197F6; border:none; height:2rem; border-radius:2px; width:35%; margin:2rem auto; display:block; color:#fff; outline:none; cursor:pointer; } .show{ font-size:1.5rem; color:#db133a; font-weight:bold; } </style> </head> <body> <h1>ตัวอย่างคุณสมบัติ DOM Input number step</h1> <p>สวัสดีครับ ป้อนตัวเลขใดๆ แล้วลองเพิ่ม/ลดค่าของมัน</p> <input type="number" class="numberInput"> <button type="button" onclick="setStep()" class="btn">ตั้งค่า step เป็น 5</button> <button type="button" onclick="getStep()" class="btn">คลิกเพื่อแสดงค่า step</button> <div class="show"></div> <script> function setStep(){ var numberInput = document.querySelector(".numberInput"); numberInput.step="5"; } function getStep() { var numberInput = document.querySelector(".numberInput"); var showMsg = document.querySelector(".show"); showMsg.innerHTML = numberInput.step; } </script> </body> </html>
ผลลัพธ์
โค้ดด้านบนจะให้ผลลัพธ์ดังนี้:

ป้อนค่าในช่องตัวเลข แล้วคลิกปุ่ม 「ตั้งค่า step เป็น 5」 เพื่อตั้งค่า step=5

ลองเพิ่ม/ลดค่าโดยใช้ปุ่ม ขึ้น และ ลง

คลิก 「คลิกเพื่อแสดงค่า step」 เพื่อดูค่า step ปัจจุบัน
