Begin HTML rows in a collapsed state as the default setting

I successfully integrated this example into my project (using Vapor Swift & Leaf) and it is functioning properly: A simple demonstration of collapsible rows in HTML/CSS/JS

However, I am looking to have all the collapsible rows start off hidden by default. My knowledge of JS is limited, so I am unsure if making changes to the following script would achieve this:

$(document).ready(function() {
    $('[data-toggle="toggle"]').change(function(){
        $(this).parents().next('.hide').toggle();
    });
});

Would I need to create a new JS script for this purpose? If so, how can I ensure it targets the correct rows to hide (and how can they be hidden)?

Answer №1

When utilizing the jQuery library, you have the ability to use a jQuery selector in order to pinpoint the specific elements that you wish to conceal and then hide them upon the document's initial load:

If all the rows you intend to toggle between hidden and shown share the common attribute of having the .hide class, you can employ this as the selector:

$('.hide').toggle();  // initiate hiding rows

If you interact with the snippet provided below, you will observe how this functionality operates. Initially, the rows remain concealed. However, upon clicking either "Accounting" or "Management," the respective rows will expand.

$(document).ready(function() {
  $('.hide').toggle();  // initially hide rows
  $('[data-toggle="toggle"]').change(function() {
    $(this).parents().next('.hide').toggle();
  });
});
table {
  width: 750px;
  border-collapse: collapse;
  margin: 50px auto;
}

th {
  background: #3498db;
  color: white;
  font-weight: bold;
}

td,
th {
  padding: 10px;
  border: 1px solid #ccc;
  text-align: left;
  font-size: 18px;
}

.labels tr td {
  background-color: #2cc16a;
  font-weight: bold;
  color: #fff;
}

.label tr td label {
  display: block;
}

[data-toggle="toggle"] {
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <thead>
    <tr>
      <th>Regian</th>
      <th>Q1 2010</th>
      <th>Q2 2010</th>
      <th>Q3 2010</th>
      <th>Q4 2010</th>
    </tr>
  </thead>
  <tbody>
    <tbody class="labels">
      <tr>
        <td colspan="5">
          <label for="accounting">Accounting</label>
          <input type="checkbox" name="accounting" id="accounting" data-toggle="toggle">
        </td>
      </tr>
    </tbody>
    <tbody class="hide">
      <tr>
        <td>Australia</td>
        <td>$7,685.00</td>
        <td>$3,544.00</td>
        <td>$5,834.00</td>
        <td>$10,583.00</td>
      </tr>
      <tr>
        <td>Central America</td>
        <td>$7,685.00</td>
        <td>$3,544.00</td>
        <td>$5,834.00</td>
        <td>$10,583.00</td>
      </tr>
    </tbody>
    <tbody class="labels">
      <tr>
        <td colspan="5">
          <label for="management">Management</label>
          <input type="checkbox" name="management" id="management" data-toggle="toggle">
        </td>
      </tr>
    </tbody>
    <tbody class="hide">
      <tr>
        <td>Australia</td>
        <td>$7,685.00</td>
        <td>$3,544.00</td>
        <td>$5,834.00</td>
        <td>$10,583.00</td>
      </tr>
      <tr>
        <td>Central America</td>
        <td>$7,685.00</td>
        <td>$3,544.00</td>
        <td>$5,834.00</td>
        <td>$10,583.00</td>
      </tr>
      <tr>
        <td>Europe</td>
        <td>$7,685.00</td>
        <td>$3,544.00</td>
        <td>$5,834.00</td>
        <td>$10,583.00</td>
      </tr>
      <tr>
        <td>Middle East</td>
        <td>$7,685.00</td>
        <td>$3,544.00</td>
        <td>$5,834.00</td>
        <td>$10,583.00</td>
      </tr>
    </tbody>
  </tbody>
</table>

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

Perform a task if a checkbox is marked or unmarked

Currently, I am working on an HTML form that includes a checkbox asking users if they have a phone number. I am facing an issue where I want to implement two actions: 1. If the user checks the checkbox, a new input element should appear on the same page ...

Is there a way to adjust the theme color in _variables.scss by recompiling Bootstrap while working with Angular and CSS?

I've been trying to update the primary color in my Angular project by editing the SCSS file node_modules/bootstrap/scss/_variables.scss. However, after stopping npm and running the front-end again, I noticed that the changes I made are not reflected i ...

Can you extract debugging details from an ajax preflight inquiry?

I am currently working on a JavaScript project that involves making an AJAX request. However, I am encountering issues with the preflight OPTIONS call for this request failing. To provide some transparency to the user, I would like to display debug infor ...

Encountering issues with JavaScript/ajax if statement functionality

Driving me insane! Dealing with 2 modal forms - one for login and another for registration. Javascript handles client-side validation, followed by an ajax call to either a registration or login php file. The PHP files return 'OK' if successful or ...

The Form button in my PHP email code is failing to produce any output when submitted

I attempted to create a contact form using a combination of HTML and PHP. Everything was functioning as expected during testing with the code input type="submit" name="submit" value="Submit" However, when I switched to using div and button with the class ...

What could be causing the ReferenceError in JSFiddle saying that the function is not defined?

Here is the code I have on jsfiddle: https://jsfiddle.net/oscar11/1xstdra1/ After running the script on jsfiddle, I encountered an error in the console: ReferenceError: doPagination is not defined. Surprisingly, when I tested it on my localhost, it worked ...

How to conceal the day picker in jQuery UI datepicker

I am facing a problem that has been addressed before. As someone with only basic knowledge of CSS, I am a bit confused here. I have two datepickers in my code. <style> .ui-datepicker-calendar { display: none; } </style> Unfortunately, ...

Using JavaScript to store CSS style properties in an array

In the midst of building a React-datagrid project, I am streamlining CSS properties for an array. However, there seems to be redundant CSS properties that I would like to consolidate into an array format. let _columns = []; let commonStyle = {"width":"20 ...

Navigation panel featuring points of interest

Greetings, my name is Kasper! I am currently working on incorporating waypoints into the sidebar of my portfolio website, a feature I just recently discovered. However, I seem to be struggling with getting it right. Here is a snippet of my code: < ...

Creating multiple tabs in PHP using the header function can be achieved by specifying the location in

I am attempting to open a new tab using Php code: header("location:print_register.php?recpt_no=".$recpt_no); In addition, I would like to open two tabs programmatically with the following code snippet: header("location:print_register.php?recpt_no=".$rec ...

What is the process of creating an additional coding workspace within Visual Studio Code?

I have been attempting to incorporate an about.html page into my website, but when I click the link nothing occurs. My goal is to connect it to a different workspace. Despite already completing the js and CSS aspects, the hyperlink for this about page re ...

display issue with inline blocks in Firefox and Internet Explorer

Issue: Difficulty with displaying inline-block in different browsers <div class="cell"> <div><img src="images/dftg.jpg" /></div> <p>Sean val</p> </div> The challenge arises when inputting a longer name ...

Retrieving error messages and status codes using Laravel and JWT authentication

One of the challenges I'm facing is implementing JWT Auth in my Laravel + Vue SPA. When checking the credentials in my Controller, the code looks like this: try { if (!$token = JWTAuth::attempt($credentials)) { return response()- ...

Tips for selecting itemid="" with CSS

Below is an example of HTML: <div itemid="MenuContainer" id="MenuContainer" class="MenuContainer"> Here is an example of CSS: #MenuContainer { /* Styles */ } Is there a way to target ItemID with CSS instead of the usual ID? ...

Retrieve the input fields from a webpage

I'm facing a challenge with a large number of form fields on my webpage, about 50 in total. Sending an AJAX request for each field seems like a daunting task, and could potentially clutter my code and make it less readable. Is there a way to efficient ...

Content not being hidden by Twitter Bootstrap tabs

I am encountering an issue with my code. When I click on an inactive tab, the content of the first tab does not hide and both tabs' content is displayed simultaneously. Is there a solution to this problem? Thank you for your help! <ul style="l ...

Using Typescript to import functions

TLDR - I need help understanding the difference between these imports in ReactJs using Typescript: setState1: (numbers: number[]) => void, setState2: Function Hello everyone, I've encountered some strange behavior when importing functions with Typ ...

Combining SVG (Raphaël) and WebGL (THREE.js) for an innovative overlay

At the moment, I am utilizing the THREE.js library to showcase a 3D scene without any difficulties. My goal is to superimpose an SVG onto the scene, but I am encountering the following issue which can be best understood through the image provided: Withou ...

Using [(ngModel)] in Angular does not capture changes made to input values by JavaScript

I have developed a custom input called formControl, which requires me to fetch and set its value using [(ngModel)]: import { Component, Injector, OnInit, forwardRef } from '@angular/core'; import { ControlValueAccessor, FormControl, NG_VALUE_ACCE ...

Is it possible to transfer elements from one array to another when clicked, but without copying the contents to the new array objects?

Welcome, For my latest project, I am excited to create a "Learning Cards" App from scratch. The concept is pretty straightforward: it consists of cards with questions. Upon clicking a button, you can reveal the correct answer. Additionally, there's a ...