How to Create Increment and Decrement Buttons with HTML, CSS, and JavaScript?

In this comprehensive tutorial, we will explore how to create Increment and Decrement Buttons using HTML, CSS, and JavaScript. This feature is widely used in web applications to enhance user interaction, especially for input fields requiring numeric values. By following this tutorial, you will gain valuable insights into implementing increment and decrement functionality effectively.

I will guide you step-by-step with practical examples and code snippets to help you achieve this feature. Whether you're building a shopping cart, a quantity selector, or any form requiring numeric inputs, this tutorial will provide a solid foundation. The techniques discussed here can also be a valuable addition to your toolkit for future projects, improving both functionality and user experience in your web applications.

Mastering this feature not only enhances your web development skills but also adds a polished and professional touch to your projects. Let’s dive in and learn how to implement these interactive buttons with ease!

Step 1:

To begin, we'll create the foundational HTML file that contains the necessary elements for our application. This file serves as the structure of our project, where we'll define the interface and key components for the increment and decrement functionality.

In this example, I save the following HTML script as index.html. Naming the file index.html is a common practice in web development, as it is typically recognized as the default entry point for most web applications. This ensures seamless accessibility when hosting the project on a web server.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3.     <meta charset="UTF-8">
  4.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  5.     <title>Increment and Decrement Number Button using HTML, CSS, and JS</title>
  6.     <link rel="stylesheet" href="style.css">
  7.  
  8.     <script src="https://code.jquery.com/jquery-3.7.1.min.js" integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" crossorigin="anonymous"></script>
  9. </head>
  10.     <div id="main-wrapper">
  11.         <h1 id="page-title"><strong>Increment and Decrement Number Button using HTML, CSS, and JS</strong></h1>
  12.         <div class="num-button-container">
  13.             <span class="num-button-dec">-</span>
  14.             <input type="number" class="num-button-inp" value="1" min="0" max="100">
  15.             <span class="num-button-inc">+</span>
  16.         </div>
  17.     </div>
  18.     <script src="script.js"></script>
  19. </body>
  20. </html>

Step 2:

Next, we'll create the CSS file to define the styling and design for our application. This file will enhance the visual appeal and user experience by applying styles to key elements such as the increment and decrement buttons and the number input field.

In this tutorial, I save the following CSS code in a file named styles.css.

  1. @import url('https://fonts.googleapis.com/css2?family=Ubuntu+Mono:ital,wght@0,400;0,700;1,400;1,700&display=swap');
  2.  
  3. * {
  4.     font-family: "Ubuntu Mono", monospace;
  5.     font-weight: 400;
  6.     font-style: normal;
  7. }
  8.  
  9. html,
  10. body {
  11.     margin: unset;
  12.     padding: unset;
  13.     width: 100%;
  14.     max-width: 100%;
  15.     height: 100%;
  16.     max-height: 100%;
  17.     overflow: auto;
  18.     background-image: linear-gradient(120deg, #a1c4fd 0%, #c2e9fb 100%);
  19.     align-items: center;
  20.     justify-content: center;
  21. }
  22.  
  23. body {
  24.     display: flex;
  25.     flex-direction: column;
  26. }
  27.  
  28. #main-wrapper{
  29.     min-width: 300px;
  30.     max-width: 500px;
  31.     padding: 15px 20px;
  32. }
  33.  
  34. #page-title{
  35.     color:#fff;
  36.     text-shadow: 0px 3px 5px #3a3a3a;
  37.     margin-bottom: 75px;
  38.     text-align: center;
  39. }
  40.  
  41. .num-button-container{
  42.     display: flex;
  43.     width: 100%;
  44.     justify-content: center;
  45.     align-items: center;
  46. }
  47.  
  48. /* Chrome, Safari, Edge, Opera */
  49. input.num-button-inp::-webkit-outer-spin-button,
  50. input.num-button-inp::-webkit-inner-spin-button {
  51.     -webkit-appearance: none;
  52.     margin: 0;
  53. }
  54.  
  55. /* Firefox */
  56. input.num-button-inp[type=number] {
  57.     -moz-appearance: textfield;
  58. }
  59.  
  60. .num-button-inp{
  61.     text-align: center;
  62.     font-size: 15px;
  63.     font-weight: 800;
  64.     user-select: none;
  65.     pointer-events: none;
  66.     cursor: not-allowed;
  67.     padding: 3px 5px;
  68.     border-radius: 0px;
  69.     border: unset;
  70.     height: 34px;
  71.     width: 50px;
  72. }
  73.  
  74. /* Input Buttons */
  75.  
  76. .num-button-dec,
  77. .num-button-inc
  78. {
  79.     display: flex;
  80.     align-items: center;
  81.     justify-content: center;
  82.     padding: 3px 3px;
  83.     font-size: 16px;
  84.     font-weight: 600;
  85.     text-align: center;
  86.     height: 30px;
  87.     width: 50px;
  88.     color: #fff;
  89.     background: #169dff;
  90.     border: 2px solid #1393ef;
  91.     cursor: pointer;
  92.     transition: all .15s ease-in-out;
  93.     user-select: none;
  94. }
  95.  
  96. .num-button-dec:hover,
  97. .num-button-inc:hover,
  98. .num-button-dec:active,
  99. .num-button-inc:active
  100. {
  101.     background: #0a72bc;
  102.     border: 2px solid #0a6aae;
  103.     font-size: 18px;
  104.  
  105. }

The provided HTML and CSS will result like the following image.

Increment and Decrement Buttons with HTML, CSS, and JavaScript

Here's the increment and decrement buttons and input field.

Increment and Decrement Buttons with HTML, CSS, and JavaScript

Step 3:

Lastly, let's create the JavaScript file for our application. This file will contain the logic that powers the functionality of the increment and decrement buttons, ensuring they operate seamlessly and update the value displayed in the input field dynamically.

I save the following JavaScript code in a file named script.js. The script.js file serves as the brain of our application, enabling users to interact with the buttons effortlessly. It captures user actions, processes the increment or decrement logic, and updates the number input field in real time. With this step, we bring the application to life, combining functionality with the structure and design established in the earlier steps.

  1. $(document).ready(function(){
  2.     $(".num-button-container").each(function(){
  3.         // Input
  4.         var inp = $(this).find("input[type='number']");
  5.  
  6.         // Max and Min value
  7.         var max = inp.attr("max") || null;
  8.         var min = inp.attr("min") || null;
  9.  
  10.         var inc_btn = $(this).find(".num-button-inc");
  11.         var dec_btn = $(this).find(".num-button-dec");
  12.  
  13.         var current_value = inp.val()
  14.  
  15.         // Increment Number
  16.         inc_btn.on("click", function(e){
  17.             e.preventDefault();
  18.             if(max != null && (parseInt(current_value) + 1) > max)
  19.                 return;
  20.             current_value++;
  21.             inp.val(current_value)
  22.         })
  23.  
  24.         // Decrement Number
  25.         dec_btn.on("click", function(e){
  26.             e.preventDefault();
  27.             if(min != null && (parseInt(current_value) - 1) < min)
  28.                 return;
  29.             current_value--;
  30.             inp.val(current_value)
  31.         })
  32.        
  33.     })
  34. })

There you have it! You can now test the functionality on your end by browsing the index.html on your preferred browser. I hope this Increment and Decrement Buttons with HTML, CSS, and JavaScript Tutorial will help you and will be useful for your future projects.

Explore more on this website for more Tutorials, Free Source Codes, and acticles covering various programming languages.

:)

Add new comment