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.

JavascriptForm Validation

Code

javascriptsnippet
17 lines
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 — document will 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

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.