105

Just wondering whether anyone knows what events an HTML5 <input type="number" /> element fires when its up / down arrows are clicked:

example number input

I'm already using an onblur for when the focus leaves the input field.

isherwood
  • 58,414
  • 16
  • 114
  • 157
Ian Oxley
  • 10,916
  • 6
  • 42
  • 49
  • This seems to be a bug of chrome https://stackoverflow.com/questions/44978087/html5-number-type-inputs-up-down-arrow-imprecise-click-bug – Oriol Jiménez Jun 26 '18 at 07:04

5 Answers5

85

change would be the event that is fired when the field's value changes.

I think the HTML5 event input would also fire.

alex
  • 479,566
  • 201
  • 878
  • 984
Jacob Relkin
  • 161,348
  • 33
  • 346
  • 320
  • 44
    only `oninput` is fired when 'up' and 'down' arrows are clicked. – N 1.1 Oct 15 '10 at 07:36
  • 2
    but onchange only fires on blur, atleast in firefox. I guess we will have to look for key press. – andho Oct 15 '10 at 07:45
  • onchange and oninput both worked in Opera, but only oninput would work in Chrome (and only then when I returned false from the event-handler - otherwise Chrome would repeatedly fire the oninput event) – Ian Oxley Oct 15 '10 at 07:51
  • @N 1.1: `oninput` IS fired when a character is insert into the field – andho Oct 15 '10 at 07:51
  • 2
    `change` does not fire when the value is typed in, only when the buttons are clicked (at least on Firefox). – Timmmm Jul 01 '15 at 08:04
  • 5
    `input` is like a fusion between `keyup change` – Thanh Trung Mar 21 '17 at 10:53
41

I found that for jQuery the following code covered keyboard input, mousewheel changes and button clicks in Chrome, and also handled keyboard input in Firefox

$("input[type=number]").bind('keyup input', function(){
    // handle event
});
Will Moore
  • 552
  • 5
  • 11
7

I found that onkeyup and onchange covered everything in Chrome 19. This handles direct value input, up down arrow keypress, clicking the buttons and scrolling the mousewheel.

onchange alone would be sufficient in Chrome, but other browsers that only render the field as a text box need the onkeyup binding, which works perfectly to read the new value.

Binding the mousewheel event separately was less successful. The event fired too early - before the field value was updated - and therefore always gave the field's previous value

Niall King
  • 71
  • 1
  • 1
5

The onchange event fires on blur but the oninput event fires as you type. Maybe you might want to put a timer on the oninput event and fire your onchange event when the user has stopped typing for a second?

andho
  • 1,166
  • 1
  • 15
  • 27
3

There is a current bug in Edge preventing change or input from firing when using the arrow keys in a number input.

Lucent
  • 1,614
  • 1
  • 19
  • 22