The functionality of displaying specific divs when multiple checkboxes are selected is currently experiencing technical issues

Within my setup, I have a selection segment which includes seven checkboxes for each day of the week: Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday. Additionally, there are two other sections featuring more checkbox options: Morning Session Section & Evening Session Section.

The functionality I am aiming for involves displaying both the Morning and Evening session sections if any day from Sunday to Friday is selected in the sessionDays checkbox group. However, if the Saturday or Sunday checkboxes are selected, only the Morning Session Section should be displayed.

I attempted to implement this behavior using the code snippet below, but encountered an issue where selecting Monday only displays the Morning and Evening sections while no other days trigger any display changes.

jQuery(function($){
      //Assigning DIVs & Fields to variable
      
        var sessionDays = $('.sessionDays');
          var sessionSunday = $('#sessionSunday');
          var sessionMonday = $('#sessionMonday');
          var sessionTuesday = $('#sessionTuesday');
          var sessionWednesday = $('#sessionWednesday');
          var sessionThursday = $('#sessionThursday');
          var sessionFriday = $('#sessionFriday');
          var sessionSaturday = $('#sessionSaturday');
        var sessionTime = $('.sessionTime');
          var sessionMorning = $('.sessionMorning');
            var eightnine = $('#eight-nine');
            var nineten = $('#nine-ten');
            var teneleven = $('#ten-eleven');
            var eleventwelve = $('#eleven-twelve');
          var sessionEvening = $('.sessionEvening');
            var fourfive = $('#four-five');
            var fivesix = $('#five-six');
            var sixseven = $('#six-seven');
            var seveneight = $('#seven-eight');

      //Hide required DIVs
      sessionEvening.hide();
      sessionMorning.hide();
      sessionDays.hide();

      $("#selectSessionType").change(function(){
        if( !$(this).val() ) {
          sessionDays.hide();
        } else {
          sessionDays.show();
        }
      }).change();

      $(sessionMonday, sessionTuesday, sessionWednesday, sessionThursday, sessionFriday).change(function() {
          if( sessionMonday.is(':checked') == true || sessionTuesday.is(':checked') == true || sessionWednesday.is(':checked') == true || sessionThursday.is(':checked') == true || sessionFriday.is(':checked') == true ) {
              sessionMorning.show();
              sessionEvening.show();
          } else if ( sessionSaturday.is(':checked') == true || sessionSunday.is(':checked') == true) {
            sessionMorning.show();
          } else {
            sessionMorning.hide();
            sessionEvening.hide();
          }
      });
  });
<link href="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

... [Additional HTML code] ...

To view a working example, visit: https://jsfiddle.net/humanware/Lpmv5vyv/

Answer №1

According to your specifications, I utilized the map function in jQuery to assign values to checkboxes. Here is the code snippet:

jQuery(function ($) {
    var sessionDays = $('.sessionDays');
    var sessionMorning = $('.sessionMorning');
    var sessionEvening = $('.sessionEvening');
    
    //Hide required DIVs
    sessionDays.hide();
    sessionMorning.hide();
    sessionEvening.hide();

    $("#selectSessionType").change(function () {
        if (!$(this).val()) {
            sessionDays.hide();
        } else {
            sessionDays.show();
        }
    }).change();

    $(".sessionDays").change(function () {
        sessionMorning.hide();
        sessionEvening.hide();
        $('.form-check-input:checked').map(function () {
            if (
                this.value == 'Monday' ||
                this.value == 'Tuesday' ||
                this.value == 'Wednesday' ||
                this.value == 'Thursday' ||
                this.value == 'Friday') {
                sessionMorning.show();
                sessionEvening.show();
                exit();
            } else if (this.value == 'Saturday' ||
                this.value == 'Sunday') {
                sessionMorning.show();
                sessionEvening.hide();
            } else {
                sessionMorning.hide();
                sessionEvening.hide();
            }
        });
    }).change();
}

HTML Code Snippet for Editing:

<div class="age underEighteen">
<h3>Under 18 Class Selection</h3>
<div class="form-row">
    <div class="form-group col-md-4">
        <label for="selectSessionType">Choose Session Type <span>*</span></label>
        <select class="form-control" id="selectSessionType">
            <option selected="selected" value="">Select Session Type</option>
            <option value="beginners">Beginners</option>
            <option value="hourly">Hourly</option>
            <option value="monthly">Monthly</option>
        </select>
    </div>
    <div class="form-group col-md-4">
        <div class="days sessionDays">
            <label>Select Session Days</label>
            <p class="text-muted">Select the specific days you want to come for training.</p>
           [checkbox inputs for days]
        </div>
    </div>
    <div class="form-group col-md-4">
        <div class="sessionTime">
            <div class="sessionMorning">
                <label>Select Session Time</label>
                [& checkbox options for morning sessions]
            </div>
            <div class="sessionEvening">
                [& checkbox options for evening sessions]
            </div>
        </div>
    </div>
</div>

Answer №2

I made a modification to the checkboxes, assigning a weekend class to Friday and Saturday, and a weekday class to the rest of the days.

UPDATE: HTML:

<div class="form-group col-md-4">
    <div class="days sessionDays">
        <label>Select Session Days</label>
        <p class="text-muted">Choose the specific days you wish to attend training.</p>
        <div class="form-check">
            <input class="form-check-input weekend" type="checkbox" value="" id="sessionSunday">
            <label class="form-check-label" for="sessionSunday">
            Sunday
            </label>
        </div>
        <div class="form-check">
            <input class="form-check-input weekday" type="checkbox" value="" id="sessionMonday">
            <label class="form-check-label" for="sessionMonday">
            Monday
            </label>
        </div>
        <div class="form-check">
            <input class="form-check-input weekday" type="checkbox" value="" id="sessionTuesday">
            <label class="form-check-label" for="sessionTuesday">
            Tuesday
            </label>
        </div>
        <div class="form-check">
            <input class="form-check-input weekday" type="checkbox" value="" id="sessionWednesday">
            <label class="form-check-label" for="sessionWednesday">
            Wednesday
            </label>
        </div>
        <div class="form-check">
            <input class="form-check-input weekday" type="checkbox" value="" id="sessionThursday">
            <label class="form-check-label" for="sessionThursday">
            Thursday
            </label>
        </div>
        <div class="form-check">
            <input class="form-check-input weekday" type="checkbox" value="" id="sessionFriday">
            <label class="form-check-label" for="sessionFriday">
                Friday
            </label>
        </div>
        <div class="form-check">
            <input class="form-check-input weekend" type="checkbox" value="" id="sessionSaturday">
            <label class="form-check-label" for="sessionSaturday">
                Saturday
            </label>
        </div>
    </div>
</div>
<div class="form-group col-md-4">
    <div class="sessionTime">
        <div class="sessionMorning">
            <label>Select Session Time</label>
            <p style="font-weight:bold;">Morning Session</p>
            <div class="form-check">
            <input class="form-check-input" type="checkbox" value="" id="eight-nine">
            <label class="form-check-label" for="eight-nine">8:00 AM - 9:00 AM</label>
            </div>
            <div class="form-check">
            <input class="form-check-input" type="checkbox" value="" id="nine-ten">
            <label class="form-check-label" for="nine-ten">9:00 AM - 10:00 AM</label>
            </div>
            <div class="form-check">
            <input class="form-check-input" type="checkbox" value="" id="ten-eleven">
            <label class="form-check-label" for="ten-eleven">10:00 AM - 11:00 AM</label>
            </div>
            <div class="form-check">
            <input class="form-check-input" type="checkbox" value="" id="eleven-twelve">
            <label class="form-check-label" for="eleven-twelve">11:00 AM - 12:00 PM</label>

                    ...

This is the corresponding jQuery code:

jQuery(function ($) {
    $('.sessionMorning').hide();
    $('.sessionEvening').hide();

    $('.weekday, .weekend').on('change', function () {
        var weekdayChecked = false;
        var weekendChecked = false;
        $('.weekday, .weekend').each(function (index, input) {
            if ($(this).hasClass('weekday') && $(this).is(':checked')) {
                weekdayChecked = true;
            }
            else if ($(this).hasClass('weekend') && $(this).is(':checked')) {
                weekendChecked = true;
            }

            if (weekdayChecked) {
                $('.sessionMorning').show();
                $('.sessionEvening').show();
            }
            else if (weekendChecked) {
                $('.sessionMorning').show();
                $('.sessionEvening').hide();
            }
            else {
                $('.sessionMorning').hide();
                $('.sessionEvening').hide();
            }

        });
    });
});

https://jsfiddle.net/4h7z5ovj/1/

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Locating function definitions in etags using Emacs

I find syntax check in js2-mode to be quite effective. However, there are times when I wish to name a function "delete" or "new" even though it may not be the best practice. Js2-mode often flags this as an error. Is there a way to use built-in keywords as ...

Troubleshooting problems with the CSS code for a progress bar in Gmail

I recently came across a unique progress bar design on Gmail and wanted to implement something similar. I found some code snippets on this webpage: . However, I encountered an issue where the progress bar was only displaying in Internet Explorer but not in ...

3D spinning icosahedron adorned with circles at each corner using three.js

In my project, I am working with an interactive icosahedron mesh that undergoes rotation. As part of the animation loop, circle geometries are dynamically added, and their locations are set to each vertex of the mesh at every frame. geometry = new THREE.I ...

The JSON.stringify() method does not update the object that has been modified by an event

Below is the code snippet: //function triggered when columns are reordered dataTable.on('column-reorder', function (e, settings, details) { var userData = tableWidget.grid('userData'); console.log(userData); //userData object s ...

Is it possible to easily extract all values associated with a particular key in a nested JSON using JavaScript?

I have a JSON structure that looks like this: [ { cells: [ { id: "1", cellType: 3, widget: { id: 1, description: "myDesc"} }, { id: "2", cellType: 4, widget: { id: 2, description: "myDesc2"} } ] }, { cells: [ { id: "3", c ...

Refreshing part of the page using AJAX

I am dealing with a page that contains multiple images, each accompanied by a text box for submitting tags related to that image. These images and text boxes are generated using partial views. While I am able to update the tags and have everything working ...

reaching an adjustable Vertical Dimension

I am currently working on the front-end development of a new project. I'm using a mix of custom CSS and Bootstrap to create a responsive user interface. The current layout adjusts nicely with the browser width, but now I need to make it responsive in ...

Having trouble with Selenium WebDriverJS on both FireFox and Internet Explorer

Having developed multiple JavaScript tests using chromedriver to run them in Chrome, I am now facing the challenge of running these same tests in FireFox and IE. The test below is functional in Chrome: var assert = require('assert'), test = requ ...

Retrieving Mouse Coordinates using Ajax in PHP

I'm wondering if it's feasible to send an Ajax request with mouse coordinates using PHP. For instance, I am fetching a page with cUrl and would like to trigger a mouse movement event on that page. At this point, I haven't written any code ...

invalid audio element

I'm currently working on building an audio player with a visualizer feature. However, when I try to initiate the audio player by clicking on the input button, my debug console keeps showing this error message: Uncaught (in promise) DOMException: Fa ...

Summing numbers with multiple queries in Angular services

One of my APIs/projects returns a JSON dataset like this: [ { "id":100, "name":"some name", "x": "y" }, { "id":200, "another name", "x": "z" } ] Another API/costs call returns a similar dataset: [ ...

Convert Python strings into HTML JavaScript blocks using Jinja2

Having trouble passing a string to an HTML page in the "<script>" block. I am currently using Python, Flask, and Jinja2. Python code: def foo(): return myString #"[{title: 'Treino 7-Corrida',start: '2015-12-08',color: '#d ...

Performing calculations on PHP and MySQL data within an HTML table, enabling precise percentage

Here's the code I'm currently working with: <td> <?php echo $row['feild1'] + $row['feild2'] + $row['feild3'] + $row['feild4'] + $row['feild5'] + $row['feild6'] + $row[' ...

Ensure that the table row filter continues to function properly even after dynamic content changes have been

I've implemented the following code to filter table rows based on text entered in an input textbox (using a solution found on this link). var $rows = $('#deviceInfoTable tr'); $('#searchVehicleName').keyup(function() { ...

Insert JavaScript code into the head of the document (including the complete script code)

I am looking to insert a script in the head section of a website so that the target HTML appears as follows: <head><script type="text/javascript">*some code...*</script></head>. This particular script works perfectly: var head = d ...

Is there a way to increase the row size only when it is clicked on?

I am facing an issue with my material-ui table. Whenever I click the button to expand row1, it also expands the second row simultaneously. How can I resolve this so that only the specific row I click on expands? <Table stickyHeader aria-label="st ...

The problem of "undefined function appendTo" is causing issues

My jQuery code looks like this: $(function() { var productTextTemplate = $('#product-text-template').html(); var productTemplate = $('product-template').html(); var product = productTextTemplate.appendTo(productTemplate); a ...

optimal application of css with jquery

I have a question about using jQuery to set the padding of a class or id. I am able to successfully change the height of an element with this code: $('.menu').css({ height: '90px' }); But now, I need to apply a specific CSS rule in jQ ...

There seems to be a hiccup in the system as we are unable

Upon visiting the URL address http://localhost:3000/test, I intend to load a log message. However, an error is returned stating "Cannot GET /test". Below is the code snippet in question: import express from 'express'; import React from 'rea ...

Retrieve the row ID when the button is clicked

I am trying to retrieve a row id from the database when a button is clicked. Below is my code in C#: protected void ddlBC_SelectedIndexChanged(object sender, EventArgs e) { LogicTableAdapters.getLvLKarTableAdapter getKar = new LogicTableAdapters.getL ...