Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

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

7 months ago · Juan Pablo Isaza
1 answers
Answer question

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>

7 months ago · Juan Pablo Isaza Report
Answer question
Find remote jobs