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

217
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 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