How to disable arrows from Number input?


In this article, we will learn how to disable and hide arrows from number-type input with the help of 2 different approaches, one using CSS, and the other using an “inputmode” attribute.

Number-type inputs are useful when we only want inputs in terms of numbers from users, like input for age, height, weight, etc. By default, the browsers show arrows in the number-type inputs that help us change (increase or decrease) the values in the input.

Let’s understand how to implement above with the help of some examples.

Example 1

In this example, we will use HTML readonly attribute to disable arrows from number input. Here, we will use the CSS ::-webkit-outer-spin-button and ::-webkit-inner-spin-button pseudo-elements to remove the default arrows. By applying the appearance: none; style, we can effectively disable the arrows from the number input.

Filename: index.html

<html lang="en">
<head>
   <title>How to disable arrows from Number input?</title>
   <style>
      input::-webkit-outer-spin-button,
      input::-webkit-inner-spin-button {
         -webkit-appearance: none;
      }
   </style>
</head>
<body>
   <h3>How to disable arrows from Number input?</h3>
   <!-- Method 1: Using HTML readonly attribute -->
   <input type="number" placeholder="Enter a number here" readonly />

</body>
</html>

Example 2

In this example, we will use JavaScript event handlers to disable arrows from number input. Here, we will use JavaScript event handlers to prevent the default behavior of keydown and wheel events. By returning false from these event handlers, we will disable the arrow keys and mouse wheel from modifying the number input value.

Filename: index.html

<html lang="en">
<head>
   <title>How to disable arrows from Number input?</title>
</head>
<body>
   <h3>How to disable arrows from Number input?</h3>
   <!-- Method 2: JavaScript event handlers -->
   <input type="number" placeholder="Enter a number here" onkeydown="return false" onwheel="return false" />

</body>
</html>

Example 3

In this example, we will use CSS "appearance" property to disable arrows from number input. Here, we will use the CSS appearance property to control the appearance of the number input. By assigning a custom class to the input and applying the appropriate styles, we can achieve the desired result of hiding the arrows.

Filename: index.html

<html lang="en">
<head>
   <title>How to disable arrows from Number input?</title>
   <style>
      .no-spinners {
         -moz-appearance: textfield;
      }
      
      .no-spinners::-webkit-outer-spin-button,
      .no-spinners::-webkit-inner-spin-button {
         -webkit-appearance: none;
         margin: 0;
      }
   </style>
</head>
<body>
   <h3>How to disable arrows from Number input?</h3>
   <!-- Method 3: CSS "appearance" property -->

   <input type="number" placeholder="Enter a number here" class="no-spinners" />

</body>
</html>

Conclusion

In conclusion, we have explored three different methods to disable and hide the arrows from number-type input fields, using CSS appearance property and pseudo elements like ::-webkit-outer-spin-button and ::-webkit-inner-spin-button, HTML readonly attribute, and javascript event handlers like listening to keydown and wheel events. These methods provide options to customize the appearance and behavior of number inputs based on specific requirements. Each method provides a different approach to achieving the same outcome, allowing developers to choose the one that best fits their project requirements and coding preferences.

Updated on: 02-Aug-2023

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements