Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

104
Vistas
How to number increment with limit

I am using number field with plus and minus for product quantity select for a project. I need to set a max limit when I press plus icon. I am using below code

$(document).ready(function() {
    $('.minus').click(function () {
        var $input = $(this).parent().find('input');
        var count = parseInt($input.val()) - 1;
        count = count < 1 ? 1 : count;
        $input.val(count);
        $input.change();
        return false;
    });
    $('.plus').click(function () {
        var $input = $(this).parent().find('input');
        $input.val(parseInt($input.val()) + 1 );
        $input.change();
       
        return false;
    });
});

How to set a maximum limit for when i press + icon.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Optimize for readability by using Math.min() and Math.max()

const MINIMUM = 1
const MAXIMUM = 99

$(document).ready(function() {
        $('.minus').click(function () {
            var $input = $(this).parent().find('input');
            var newCount = Math.max(parseInt($input.val()) - 1, MINIMUM);
            $input.val(newCount);
            $input.change();
            return false;
        });
        $('.plus').click(function () {
            var $input = $(this).parent().find('input');
            var newCount = Math.min(parseInt($input.val()) + 1, MAXIMUM)
            $input.val(newCount);
            $input.change();
            return false;
        });
    });
about 4 years ago · Juan Pablo Isaza Denunciar

0

Here's how you'd limit it to 99 for example :

$(document).ready(function() {
    $('.minus').click(function () {
        var $input = $(this).parent().find('input');
        var count = parseInt($input.val()) - 1;
        count = count < 1 ? 1 : count;
        $input.val(count);
        $input.change();
        return false;
    });
    $('.plus').click(function () {
         var $input = $(this).parent().find('input');
        var count = parseInt($input.val()) + 1;
        count = count > 99 ? 99 : count;
        $input.val(count);
        $input.change();
        return false;
    });
});
about 4 years ago · Juan Pablo Isaza Denunciar

0

You need to check the current value. If the value exceeds the limit, ignore the action:

$(document).ready(function() {
        $('.minus').click(function () {
            var $input = $(this).parent().find('input');
            var count = parseInt($input.val()) - 1;
            count = count < 1 ? 1 : count;
            $input.val(count);
            $input.change();
            return false;
        });
        $('.plus').click(function () {
            var $input = $(this).parent().find('input');

            if ($input.val() > 100) {
                return;
            }

            $input.val(parseInt($input.val()) + 1 );
            $input.change();
           
            return false;
        });
    });
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda