stepUp And stepDown Drive Number Inputs
stepUp and stepDown move a number input by its step attribute and clamp at min and max, keeping the value valid without manual math.
Code
const qty = document.createElement('input');
qty.type = 'number';
qty.min = '0';
qty.max = '10';
qty.step = '2';
qty.value = '4';
console.log('start:', qty.value, 'step:', qty.step);
// stepUp/stepDown move by step, clamped to min and max
qty.stepUp();
qty.stepUp();
console.log('after two stepUp:', qty.value);
qty.stepDown(3);
console.log('after three stepDown:', qty.value);
console.log('valid after clamping:', qty.checkValidity());Inside the snippet
Top-level bindings qty hold the demo state — nothing sits inside a function scope.
Points of care
- Ready as-is on a bare install of the language.
- This is browser-side code —
documentwill not resolve in Node.
Putting it to use
The run emits 4 labeled sections — start:, after two stepUp:, after three stepDown:, valid after clamping: — one per step.
Core lines under the microscope
const qty = document.createElement('input');
qty.type = 'number';
qty.value = '4';
Interactive Tools for This Topic
JSON to Zod Schema ConverterInfer typed Zod z.object schemas from JSON payloads locally, supporting nested objects, merged array shapes, enums, nullable fields, and JSDoc comments.JSON Schema ValidatorValidate JSON data against a JSON Schema definition and get detailed error reports. Supports type checking, required fields, and common constraint validation.JSONL / NDJSON InspectorValidate NDJSON and JSON Lines line-by-line, pinpoint syntax errors by line number, analyze key frequencies and types, and export a pretty JSON array.Credit Card Brand DetectorIdentify the card network from a number's prefix and length, validate the Luhn checksum, and show a masked form and the issuer identification number.
FAQ
- Does stepUp And stepDown Drive Number Inputs run in plain Node.js?
- No — it touches `document`, which exist only in the browser. Run it in a page (or the DevTools console) instead of a bare Node process.
Report a problem with this exampleOpens a pre-filled issue on GitHub.