toggle the visibility of a div using an unordered list

One of my challenges involves toggling the visibility of a div (filter-panel--content) containing an unordered list when clicking on the label above it.

#LABEL/DIV THAT CAN BE CLICKED AND TRIGGERS .click()
<div class="filter-panel--flyout">
   <div id="label-it" class="label-it"><label class="filter-panel--title">
      <h3 class="rums">Ports</h3><div id="klapp" class="klapp"></div>
   </label></div>

#DIV TO SHOW/HIDE WHEN LABEL IS CLICKED
<div class="filter-panel--content">
   <ul class="filter-panel--option-list">
      <li class="filter-panel--option">
      <div class="option--container">
         <span class="filter-panel--checkbox">
         <input type="checkbox" id="__f__575" name="__f__575" value="575">
         </span>
      <label class="filter-panel--label" for="__f__575">8</label>
      </div>
      </li>
   </ul>
</div></div>

#LABEL/DIV THAT CAN BE CLICKED AND TRIGGERS .click()
<div class="filter-panel--flyout">
   <div id="label-it" class="label-it"><label class="filter-panel--title">
      <h3 class="rums">Ports</h3><div id="klapp" class="klapp"></div>
   </label></div>

#DIV TO SHOW/HIDE WHEN LABEL IS CLICKED
<div class="filter-panel--content">
   <ul class="filter-panel--option-list">
      <li class="filter-panel--option">
      <div class="option--container">
         <span class="filter-panel--checkbox">
         <input type="checkbox" id="__f__576" name="__f__576" value="576">
         </span>
      <label class="filter-panel--label" for="__f__576">9</label>
      </div>
      </li>
   </ul>
</div>

#LABEL/DIV THAT CAN BE CLICKED AND TRIGGERS .click()
<div class="filter-panel--flyout">
   <div id="label-it" class="label-it"><label class="filter-panel--title">
      <h3 class="rums">Ports</h3><div id="klapp" class="klapp"></div>
   </label></div>

#DIV TO SHOW/HIDE WHEN LABEL IS CLICKED
<div class="filter-panel--content">
   <ul class="filter-panel--option-list">
      <li class="filter-panel--option">
      <div class="option--container">
         <span class="filter-panel--checkbox">
         <input type="checkbox" id="__f__577" name="__f__577" value="577">
         </span>
      <label class="filter-panel--label" for="__f__577">10</label>
      </div>
      </li>
   </ul>
</div>

In my click() function, I am trying to toggle classes and want to also hide or show the clicked filter-panel--content:

$('.label-it').click(function() {
  $(this).find(".filter-panel--title div").toggleClass('klapp klappe');
  // Code to hide or show filter-panel--content should go here
});

However, I need assistance with only closing the clicked label's filter-panel--content without affecting others. Can someone provide guidance? Thank you!

-- edit:

I have another question regarding the toggle functionality

$(this).closest(".filter-panel--flyout").find(".filter-panel--content div")
. When I submit inputs, the display property is refreshed causing it to revert to display:block. How can I ensure that the toggled .filter-panel--content remains in its toggled state even after submissions?

Answer №1

It's not possible to utilize

$(this).find(".filter-panel--content div")
because filter-panel--content is not a descendant of label-it.

To resolve this issue and make it function correctly, you need to utilize .closest() like so:

$(this).closest(".filter-panel--flyout").find(".filter-panel--content div")

Check out the demonstration below:

$('.label-it').click(function() {
  $(this).find(".filter-panel--title div").toggleClass('klapp klappe');
  $(this).closest(".filter-panel--flyout").find(".filter-panel--content").toggle();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="filter-panel--flyout">
  <div id="label-it" class="label-it"><label class="filter-panel--title">
      <h3 class="rums">Ports</h3><div id="klapp" class="klapp"></div>
   </label>
  </div>

  #DIV WHICH SHOULD show AND hide WHEN LABEL IS CLICKED
  <div class="filter-panel--content">
    <ul class="filter-panel--option-list">
      <li class="filter-panel--option">
        <div class="option--container">
          <span class="filter-panel--checkbox">
         <input type="checkbox" id="__f__575" name="__f__575" value="575">
         </span>
          <label class="filter-panel--label" for="__f__575">8</label>
        </div>
      </li>
    </ul>
  </div>
</div>

#LABEL/DIV WHICH CAN BE CLICKED AND THE .click() SHOULD FIRE
<div class="filter-panel--flyout">
  <div id="label-it" class="label-it"><label class="filter-panel--title">
      <h3 class="rums">Ports</h3><div id="klapp" class="klapp"></div>
   </label>
  </div>

  #DIV WHICH SHOULD show AND hide WHEN LABEL IS CLICKED
  <div class="filter-panel--content">
    <ul class="filter-panel--option-list">
      <li class="filter-panel--option">
        <div class="option--container">
          <span class="filter-panel--checkbox">
         <input type="checkbox" id="__f__576" name="__f__576" value="576">
         </span>
          <label class="filter-panel--label" for="__f__576">9</label>
        </div>
      </li>
    </ul>
  </div>
</div>

#LABEL/DIV WHICH CAN BE CLICKED AND THE .click() SHOULD FIRE
<div class="filter-panel--flyout">
  <div id="label-it" class="label-it"><label class="filter-panel--title">
      <h3 class="rums">Ports</h3><div id="klapp" class="klapp"></div>
   </label></div>

  #DIV WHICH SHOULD show AND hide WHEN LABEL IS CLICKED
  <div class="filter-panel--content">
    <ul class="filter-panel--option-list">
      <li class="filter-panel--option">
        <div class="option--container">
          <span class="filter-panel--checkbox">
         <input type="checkbox" id="__f__577" name="__f__577" value="577">
         </span>
          <label class="filter-panel--label" for="__f__577">10</label>
        </div>
      </li>
    </ul>
  </div>
</div>

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

Limiting the space character in a password field with regular expressions in JavaScript

Currently, I am facing the challenge of implementing a RegEx expression on a property within a model on an MVC website that I am currently developing. While I understand the individual components of the expression, I am struggling to combine them effectiv ...

Selecting a date in Jade's date picker

I'm currently facing an issue with getting a datepicker to function properly using Jade in a node.js and express framework. Within index.jade, I am loading the necessary javascript files like this: link(type='text/css', href='css/ui-l ...

Tips on adding CSS to a React component

I've successfully converted an HTML file into a component designed to resemble a 7-segment LED display. I have imported the necessary CSS styles from the index.css file, but I am unsure how to properly apply these styles within the component. My atte ...

The SetTimeout tooltip will only appear when the mouse is moved

Utilizing the Cool DHTML tooltip script provided by © Dynamic Drive DHTML code library, I have customized it to display a tooltip that I created. To ensure that the Tooltip does not appear instantly, I have incorporated a simple code modification: Upon ...

I need assistance in validating and processing an HTML form that takes input from a UI login interface while utilizing the Fiber framework in the Go programming language

Can you assist me in capturing input data using the Fiber Golang framework? <form action="/" method="POST" novalidate> <div> <p><label>Email Address:</label></p> <p><inp ...

Center a vertically aligned element following a heading, with the content dynamically generated and enclosed in a fixed container

My mind is racing with this problem and I'm not sure if it's solvable, but before throwing in the towel, I thought I'd seek help from the Internet Gods. Here's a visual representation of what I'm aiming for: I want the text to al ...

Express.js: Request body with an undefined base64 encoding

I'm currently working on transforming a table in .xls format to individual rows in .csv format. I came across a helpful library for this task called XLSX My initial step involves encoding the .xls table into base64 format. Next, I'm attempting ...

get value from json with specified key

Here is a function that handles the success of my AJAX call: success: function(responseJson) { var receivedData = []; $.each(responseJson.jsonArray, function(index) { $.each(responseJson.jsonArray[index], function(key, value) ...

Issue encountered when trying to import @tensorflow/tfjs-node in conjunction with the face-api.js library within a node.js environment

Using the @tensorflow/tfjs-node package along with face-api.js to enhance performance, here is my code snippet: // Import nodejs bindings to native tensorflow, // Not required but will significantly speed up operations (Python required) require('@tens ...

Troubleshooting Google Analytics on Localhost: Investigating Issues with Test Code

I've been working on an HTML file and wanted to integrate Google Analytics into it. After signing up with GA, I received a tracking code to insert into my HTML file within the head section at the very beginning. I followed the instructions and then pr ...

Discovering the audio file URL hidden within javascript code

Is it possible to programmatically locate a link to an audio pronunciation clip on a website? I am in the process of creating a personalized language learning Anki deck. The specific site I am referring to is: When clicking on "Framburður," the audio cli ...

Do the jQuery fadeIn() and animation() functions operate in a non-blocking manner?

When my page loads, it initiates multiple ajax queries using $('document').ready(). I want to incorporate fadeIn() or animation() to briefly display some information after the first ajax call is received. Would the subsequent js/ajax calls be he ...

Tips for organizing multiple TextField components within a Grid container using Material-UI

I utilize Material-UI for my front-end design needs. I have a query related to the Grid layout feature. My goal is to include 7 TextField elements, but they are appearing overlapped. When I modify all 7 TextField elements from xs={1} to xs={2}, they become ...

Retrieve the text content of a span element using jQuery

How can I use jQuery to extract the text "cnn.com" from the given HTML structure? var txt = $(".example1 >.myNewTest").closest('.subexample2').find('span').text(); <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3 ...

A step-by-step guide on fetching multiple iframe contents simultaneously with Selenium in Java

Today, I am facing an interesting challenge. I need to extract the page source of a webpage that includes an iframe. Surprisingly, when using the PhantomJSDriver with the code snippet below, I am only able to retrieve either the page content or the iframe ...

Utilizing an external SCSS or CSS file with Material UI in React: A step-by-step guide

Encountering issues with external CSS files in my React project that uses Material UI components. Despite creating a CSS file for each component in the react components folder, they fail to load upon hard reloading the site. I prefer using external CSS ov ...

What are the consequences of excluding a callback in ReactJs that may lead to dysfunctionality?

<button onClick = { () => this.restart()}>Restart</button> While going through a ReactJs tutorial, I encountered a game page which featured a restart button with the code snippet mentioned above. However, when I tried to replace it with the ...

guide to importing svg file with absolute path

I have been attempting to load SVG files from my LocalDrive using an absolute path. Despite successfully achieving this with a relative path, the same method does not work when utilizing an absolute path. <script> $(document).ready(functio ...

Looking to either include a div around fancybox on click or apply a class to the body

My goal is to incorporate multiple fancyboxes on a single page, each opening with either an iframe in an iPhone, iPad, or desktop browser upon clicking. Although I have successfully displayed the image on the default overlay, I am facing difficulty in add ...

How can Codeception/Selenium help in testing the tooltip or customValidity message?

I am trying to implement a feature in my Codeception scenario where I want to validate a form submission with user errors, such as confirming a wrong email address, and display a custom tooltip message in the browser. HTML <form ... > <label ...