Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

105
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!