What events does an <input type="number" /> fire when its value is changed?

JavascriptHtml

Javascript Problem Overview


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.

Javascript Solutions


Solution 1 - Javascript

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

I think the HTML5 event input would also fire.

Solution 2 - Javascript

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
});

Solution 3 - Javascript

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

Solution 4 - Javascript

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?

Solution 5 - Javascript

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionIan OxleyView Question on Stackoverflow
Solution 1 - JavascriptJacob RelkinView Answer on Stackoverflow
Solution 2 - JavascriptWill MooreView Answer on Stackoverflow
Solution 3 - JavascriptNiall KingView Answer on Stackoverflow
Solution 4 - JavascriptandhoView Answer on Stackoverflow
Solution 5 - JavascriptLucentView Answer on Stackoverflow