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

201
Vistas
How to check multiple selectors to trigger an event?

New to jquery but I am working on a responsive calendar. Now I have a start date and an end date field. If the end date is populated, the reoccurrence check box shows as it removes it from the not-visible class.

How can I add a check on the start date also? I want the check box to show when both of the start and end fields are filled not just one.

 $("#end_date").on("input", function (){
            if($('#end_date').length >= 1) {
                $("#reoccurrence").removeClass('not-visible');
            } else {
                $("#reoccurrence").addClass('not-visible');
            }
        });
.not-visible {
    display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="col">
  <div class="md-form md-form-container-fix">
    <input type="text" id="start_date" class="form-control date-picker" name="start_date" required />
    <label for="start_date">Start Date</label>
  </div>
</div>

<div class="col">
  <div class="md-form md-form-container-fix">
    <input type="text" id="end_date" class="form-control date-picker" name="end_date" required />
    <label for="end_date">End Date</label>
  </div> 
</div>

<div class="form-check not-visible" id="reoccurrence" name="reoccurrence">
  <input class="form-check-input" type="checkbox" id="is_reoccurring" name="is_reoccurring" value="1" />
  <label class="form-check-label" for="is_reoccurring"> Reoccurrence? </label>
</div>

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

In this context, using length will tell you whether that input exists in the DOM (0 = doesn't exist, 1 = exists), but it won't evaluate the values of the inputs.

length
The number of elements in the jQuery object.
The number of elements currently matched.

Instead, I recommend using val() to verify that the values of both inputs are not empty strings.

In the demonstration below, I define each input and the "reoccurrence" element as variables. I use add() to combine the two inputs and bind the event handler to both. Alternatively, you could simply select both inputs: $("#start_date,#end_date"); see multiple selector.

Upon input, I define a boolean "oneEmpty" variable based on the values of both inputs. Then, I use toggleClass() to "add or remove one or more classes ... depending on ... the value of the state argument."

When either input is empty, the "oneEmpty" state is true and the "not-visible" class is toggled on. When both inputs are filled, the state is false and the class is toggled off.

var $start_date = $('#start_date');
var $end_date = $('#end_date');
var $reoccurrence = $("#reoccurrence");

$start_date.add($end_date).on("input", function() {
  let oneEmpty = $start_date.val() == '' || $end_date.val() == '';
  $reoccurrence.toggleClass('not-visible', oneEmpty);
});
.not-visible {
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="col">
  <div class="md-form md-form-container-fix">
    <input type="text" id="start_date" class="form-control date-picker" name="start_date" required />
    <label for="start_date">Start Date</label>
  </div>
</div>

<div class="col">
  <div class="md-form md-form-container-fix">
    <input type="text" id="end_date" class="form-control date-picker" name="end_date" required />
    <label for="end_date">End Date</label>
  </div>
</div>

<div class="form-check not-visible" id="reoccurrence" name="reoccurrence">
  <input class="form-check-input" type="checkbox" id="is_reoccurring" name="is_reoccurring" value="1" />
  <label class="form-check-label" for="is_reoccurring"> Reoccurrence? </label>
</div>

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