Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

122
Visualizações
Calculate with minus/plus buttons

(Edit : up)

First I'm a JS beginner. I need to make a calcul using both input type number and plus/minus buttons linked to it (to replace spinners on smartphone screens). I precise I can't use JQuery or any other external library.

I face 3 problems :

  1. if I don't first focus the input, the "+" button does not work (the function is not called).
  2. once the input has been focused at least once, I can increase/decrease its value with buttons. But if I change the input value by clicking into it and entering a new value with keyboard, 1 click on the buttons increase/decrease several times. 2 times, 5 times, 7 times. I've checked with console.log, the function is then called several times.
  3. the calcul is not done when using the buttons.

I've spent hours and hours to try to solve... thanks in advance.

Here is the code :

<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>title</title>
    <style>
        .result {
            width: 5rem;
            height: 2.5rem;
            background-color: grey;
        }
    </style>
</head>
<body>
    
    <form name="myForm">

        <div class="result">
            <output id="output"></output>
        </div>

        <div>
            <input type="button" value="-" id="decreaseMe">
            <input type="number" id="saisie">
            <input type="button" value="+" id="increaseMe">
        </div>

    </form>

    
    <script>
        var valeur = 10;
        var varSaisie = document.querySelector('#saisie'); 
        var varOutput = document.querySelector('#output'); 
      
        myForm.addEventListener('input', function () {
            
        decreaseMe.addEventListener('click', function(){
        varSaisie.value = --varSaisie.value;
        });

        increaseMe.addEventListener('click', function(){
        if(varSaisie.value === "")
        {
            varSaisie.value = 0;
        }
        varSaisie.value = ++varSaisie.value;
        });

        varOutput.value = parseFloat(varSaisie.value) + parseFloat(valeur);

        }, false);

    </script>
</body>
</html>
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

It sounds like something is blocking the click from reaching the plus button. You shouldn't need to focus on it for it to work. it might be behind something. try to move it to be the top element on the page by adding this style to the page:

<style>
   #increaseMe {
      z-index:99;
   }
</style>

Please comment if this fixed this issue.

about 4 years ago · Juan Pablo Isaza Relatório

0

Finally I've found the solution. I share it in case it's useful for community. Not sure it's best practice but it does the job.

<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Formulaire avec boutons plus moins</title>
    <style>
        .result {
            width: 5rem;
            height: 2.5rem;
            background-color: grey;
        }

    </style>
</head>
<body>
    <form name="myForm">
        <div class="result">
            <output id="output"></output>
        </div>
        <div>
            <input type="button" value="-" id="decreaseMe">
            <input class=boxnumber type="number" id="saisie" min="0" max="95" step="1" placeholder="0-95">
            <input type="button" value="+" id="increaseMe">
        </div>
    </form>
    
    <script>
        var valeur = 10;
        var varSaisie = document.querySelector('#saisie'); 
        var varOutput = document.querySelector('#output'); 

        myForm.addEventListener('input', function(){
            myResult();
        });

        decreaseMe.addEventListener('click', function(){
           varSaisie.value = --varSaisie.value;
           myResult();
        });

        increaseMe.addEventListener('click', function(){
           varSaisie.value = ++varSaisie.value;
           myResult();
        });

        function myResult() {
           varOutput.innerHTML = parseFloat(varSaisie.value) + parseFloat(valeur);
        }
    </script>
</body>
</html>
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda