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

193
Visualizações
Enable submit button when all OTP input fields are entered

I have the following html and the javascript

<div class="SMS" id="otp">
    <div class="SMS-wrapper">
        <div class="SMSItem otp">
            <input minlength="1" maxlength="1" name="OTPcode" autocomplete="off" class="SMS_Input" pattern="[0-9]+" type="number" id="code1" onkeyup="onkeyUpEvent(1,event)">             
        </div>
        <div class="SMSItem otp">
            <input minlength="1" maxlength="1" name="OTPcode" autocomplete="off" class="SMS_Input" pattern="[0-9]+" type="number" id="code2" onkeyup="onkeyUpEvent(1,event)">             
        </div>
        <div class="SMSItem otp">
            <input minlength="1" maxlength="1" name="OTPcode" autocomplete="off" class="SMS_Input" pattern="[0-9]+" type="number" id="code3" onkeyup="onkeyUpEvent(1,event)">             
        </div>
        <div class="SMSItem otp">
            <input minlength="1" maxlength="1" name="OTPcode" autocomplete="off" class="SMS_Input" pattern="[0-9]+" type="number" id="code4" onkeyup="onkeyUpEvent(1,event)">             
        </div>
    </div>
</div>
<div id="divButtons" class="buttonContainer" data-kaction="submitDiv" style="display:block">
    <input type="button" name="btnOTP" id="btnOTP" value="submit" class="btn-primary submitbutton disabled">
</div>

And I have written the following javascript to enable the submit button when all the OTP fields are entered . But the submit button is still disabled. I am trying to figure out the solution and learn in the process.

$(document).ready(function() {
  var filled = true;
  $('.otp').change(function() {
    $('#otp input[type=number]').each(function() {
      if($(this.val() == '') {
        filled = false;
       }
    });
    return filled;
    
    if (filled) {
      $('#btnOTP').removeClass('disabled');
    } else {
      $('#btnOTP').addClass('disabled');
    }
  })
});
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

be careful when using return it will exit function, i also changed event call because .change is deprecated

  $('.otp').on('change', function () {
    let filled = true
    
    $('#otp input[type=number]').each(function () {
      if ($(this.val() == '') )
        filled = false;
      
    });

    if (filled) {
      $('#btnOTP').removeClass('disabled');
    } else {
      $('#btnOTP').addClass('disabled');
    }

})
about 4 years ago · Juan Pablo Isaza Relatório

0

Few corrections:

  1. Fix typo in if
  2. Remove early return
  3. Early exit if empty is met
$(document).ready(function() {
    var filled = true;

    $('.otp').change(function() {
        $('#otp input[type=number]').each(function() {
            if ($(this.val() == '')) {
                filled = false;
                return false;
            }
        });

        if (filled) {
            $('#btnOTP').removeClass('disabled');
        } else {
            $('#btnOTP').addClass('disabled');
        }
    })
});
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