question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[TextField] How to prevent mousewheel in input of type number?

See original GitHub issue

Problem description

Scrolling in a number field changes the values. In all the applications I have built so far people have asked me to prevent this behaviour because it sometimes causes corrupted data.

See also https://stackoverflow.com/questions/9712295/disable-scrolling-on-input-type-number/38589039#38589039

Steps to reproduce

Build a TextField of type number. Set focus into it. Scroll with your mousewheel.

Versions

This happens in all browsers in input elements, as far as I know.

Workaround

I have tried this:

document.addEventListener('mousewheel', function(event) {
  if (window.document.activeElement.type === 'number') {
    event.preventDefault()
  }
})

But it only seems to work sometimes. No Idea why it only works inconsistently.

Request

Now I see that changing standard behaviour would not be good. But adding this as an option would be very nice.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:20 (6 by maintainers)

github_iconTop GitHub Comments

38reactions
yakubova92commented, Jan 14, 2021

onWheel={event => event.target.blur()} works, using onWheel={event => event.currentTarget.blur()} does not work

12reactions
dennis-gonzalescommented, Mar 24, 2022

onWheel={event => event.target.blur()} works, using onWheel={event => event.currentTarget.blur()} does not work

Works for me, thanks. A little bit of modification for typescript

onWheel={e => e.target instanceof HTMLElement && e.target.blur()}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Disable scrolling on `<input type=number>` - Stack Overflow
Prevent the default behavior of the mousewheel event on input-number elements like suggested by others (calling "blur()" would normally not ...
Read more >
How do I prevent the mousewheel from incrementing and ...
$('input[type=number]').on('wheel', function(e){ return false; }); Maybe you need to adapt it, I can't reproduce that behavior.
Read more >
How to disable scroll to change number in <input type ...
How to disable scroll to change number in <input type=”number”> field using JavaScript/jQuery? · Select the element. · Add an event to that ......
Read more >
Disable Number Input Spinner & Scroll - CodePen
Adding Classes. In CodePen, whatever you write in the HTML editor is what goes within the <body> tags in a basic HTML5 template....
Read more >
Disable scroll on input type number - Javascript - AdnanTech
target object. All input fields in HTML5 have a function named “blur()” which removes the focus from that input field. Calling this function ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found