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

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

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 Report

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