Each time a nested collapse occurs, it triggers its parent collapse

I have come across some similar answers, but they all pertain to accordions, which I am not utilizing in this case.

In my code, I have nested collapse items. You can view the code snippet here on CodePen

$('#collapseOutter').on('show.bs.collapse', function(event) {
  $('#collapseBtn').html("Show Less");
  console.log('This only occurs when the outer div is opened');
  event.stopPropagation();
  console.log('Even with stopping propagation, it still fires');
})
$('#collapseOutter').on('hide.bs.collapse', function() {
  $('#collapseBtn').html("Show More");
  console.log('This only happens when the outer div is collapsed');
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0/js/bootstrap.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
  <div class="row ml-4">
    <div class="row header-div">
      <a id="collapseBtn" class="btn-info btn" data-toggle="collapse" href="#collapseOutter" role="button" aria-expanded="false" aria-controls="collapseOutter">
  Show More
</a>
    </div>
  </div>
  <div class="row ml-4">
    <div id="collapseOutter" class="collapse">
      <p>Here is a paragraph about the data you're about to see.</p>
      <div class="row">
        <div id="item-a">
          <a id="item-a" class="" data-toggle="collapse" href="#collapseInnerA">
Item One of a list
</a>
          <br />
          <div id="collapseInnerA" class="collapse">
            <div class="row">
              <p class="ml-4">Item One Related Information</p>
            </div>
          </div>
        </div>
      </div>
      <div class="row">
        <div id="item-b">
          <a id="item-b" class="" data-toggle="collapse" href="#collapseInnerB">
Item Two of a list
</a>
          <br />
          <div id="collapseInnerB" class="collapse">
            <div class="row">
              <p class="ml-4">Item Two Related Information</p>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

Whenever I click on the list items inside the first collapse, it triggers the parent methods. How can I prevent this from happening and specifically handle it within Bootstrap?

Answer №1

Prevent the event propagation at the inner collapses instead of waiting for it to reach the outer collapse. Check out this codepen example where the first inner collapse's event bubbles while the second one is successfully stopped.

$('#collapseInnerB').on('show.bs.collapse', function( event ) {
    event.stopPropagation();
})
$('#collapseInnerB').on('hide.bs.collapse', function( event ) {
    event.stopPropagation();
})

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

Attempting to select an input field

I'm having trouble clicking on the Select Files button that is nested within a SPAN and Input Tag. Despite using Xpath, Id, and Name, I am unable to successfully click on the button. <span> Select <u>a</u> Files... </span> & ...

Encountering Problems with Ajax Refresh During Page Initialization

I'm currently experiencing issues with loading content on page load using ajax. The ajax is successfully calling the correct action in the corresponding controller. The initial part of the action code, where I update the database, is functioning as ex ...

Exploring the Relationship Between Z-Index and Overflow in a Collapsible Div Featuring Slim Select

Here is my demonstration of embedding a Slim Select dropdown within a collapsible div: CodePen Example <html> <head> <!-- MULTIPLE SELECT DROPDOWNS --> <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ ...

As my character slides off the moving platform in this exciting Javascript canvas game, my heart

Can anyone help me figure out how to keep my player on the moving platform? I'm not sure if I need to add gravity or something else. I'm still learning the ropes. export function checkTopCollision({ item1, item2 }) { return ( item1.y + item ...

Node.js provides a straightforward way to decode HTML code

Can strings with HTML-encoded characters like &#39;, &amp;, etc., be converted into plain character strings without using multiple str.replace(/&#39;/g, "'") statements? ...

Adjust the panel size accordingly for smaller screens

My application is utilizing the Spotify API to retrieve names and images. These are then displayed on my webpage within cards/panels in the following format: <div class="col-md-4" v-if="type == 'tracks'" v-for="(track, index) in tracks"> ...

Troubleshooting Issues with ASP.NET MVC5 CRUD Validation Inside Modal

My modal validation is functioning correctly, however, I am experiencing an issue where the form always returns valid()=false resulting in not hitting the Controller Method. I have attempted to use the unobtrusive.parse method for validation and it works ...

Horizontal rule located within a table but spanning the entire width

I wrote the following code: <table> {item.awards.map((obj,i) => <tbody> <tr> <td>Name</td> <td>:</td> <td>{obj.title}</td> </tr> ...

What is the best way to show a loading spinner as my Next image component loads, especially when there is an existing image already being displayed?

I'm trying to incorporate a loading spinner or any other HTML element while an image is being loaded. Currently, I am utilizing the ImageComponent to showcase images in a random movie generator. Although I managed to make the spinner work for the init ...

Utilizing jQuery to Record the Status of HTML5 Forms

Can jQuery be used to track the status of an HTML5 form element? For instance, if a user enters an invalid email address in the email field, can jQuery detect this event and take action such as disabling the submit button until a valid email is entered? ...

Rails datepicker now automatically saves the current date

Hi there! I'm facing an issue when trying to save a specific date using datepicker. Instead of saving the date I entered, it is saving today's date. My start_time includes both date and time. Below is my form: <%= f.label :start_time, &apos ...

How can we have a div stay at the top of the screen when scrolling down, but return to its original position when scrolling back up?

By utilizing this code, a div with position: absolute (top: 46px) on a page will become fixed to the top of the page (top: 0px) once the user scrolls to a certain point (the distance from the div to the top of the page). $(window).scroll(function (e) { ...

Top solution for live chat between server and client using jQuery, Java, and PHP on a localhost setup

Seeking recommendations for the best chat solution to facilitate communication between client PCs and a server in my private network. Specifically interested in an Ajax/Java solution similar to the chat support feature found in GMail. ...

Is it time to use JQuery post-DocumentReady?

After all the $(document).ready scripts have run, is there a way to register an event that will be executed? I have code that must run after the entire page, including jQuery scripts, has finished loading. Is this achievable? I have to register this in a ...

The integration of CSS into Laravel was unsuccessful

Below is the content of my index file located in views.admin: <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet&qu ...

Issue with file upload process in php

I'm currently facing an issue with my file upload function. I've been referring to a website guide for creating the upload form, but made some modifications to it. Here is the code snippet: upload_form.php : //the jquery script is still the sam ...

Retrieving the value of the option that has been selected from a dropdown

I have successfully implemented a drop-down menu with the following code: // Creating a select element for priority within a form in the bottom row div const formPriority = document.createElement("select"); formPriority.setAttribute("name","project"); form ...

Instantly magnifying on the initial point regardless of the zoom type chosen is a feature of Chart.js zoom

Despite adding zoom functionality to my line chart, I am facing an issue where it automatically zooms in to the first point and does not allow me to zoom back out, except for using the reset zoom function. The zoom out function is also not working properly ...

Display "Temporary Text" in an HTML drop-down list if no selection is made

Is there a way to ensure that the placeholder in an existing select element always remains visible? I noticed that when I select an option and then go back to the blank item, the placeholder disappears. How can I make sure it is always displayed when no ...

Adhering button for sliding side panel

Check out my JSFiddle HERE to see what I have done. I would really appreciate it if someone could help me figure out how to make the show button float with the sidr panel :) <style type="text/css"> #panel { position: fixed; top: 50%; r ...