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

216
Visualizações
Javascript: Hide .div if two inputs have a value

I have two inputs with classes (q1, q2) and a div with class (output). I would like to be able to hide the div when one or all of the inputs have "any value". I'm new to javascript.

$(document).ready(function() {
  $('.q1, .q2').keyup(function() {
    if ($(this).val().length == 0) {
      $('.output').show();
    } else {
      $('.output').hide();
    }
  }).keyup();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="output">Test</div>
<input type="text" value="" class="q1">
<input type="text" value="" class="q2">

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You need to check both input values to achieve your goal like this:

$(document).ready(function() {
  $('.q1, .q2').keyup(function() {
    var q1 = $('.q1').val();
    var q2 = $('.q2').val();

    if (q1.length || q2.length) {
        $('.output').hide();
    } else {
        $('.output').show();
    }
  }).keyup();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="output">Test</div>
<input type="text" value="" class="q1">
<input type="text" value="" class="q2">

about 4 years ago · Juan Pablo Isaza Relatório

0

Haseeb has given a great answer.

Just wanted to mention as you're new to JQuery / Javascript it's often worth checking the existence of classes before using them, as it prevents changes to the HTML further down the line causing unexpected issues.

$(document).ready(function() {
     // Check if the classes exist here
     if ($(".q1")[0] && $(".q2")[0] ) {
          $('.q1, .q2').keyup(function() {
            var q1 = $('.q1').val();
            var q2 = $('.q2').val();
        
            if (q1.length || q2.length) {
                $('.output').hide();
            } else {
                $('.output').show();
            }
          }).keyup();
        }
    });
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