Is there a way to trigger an action immediately after a collapse item is closed?

According to the Bootstrap 5 documentation, you can instantiate a collapse using the constructor. For example:

var myCollapse = document.getElementById('myCollapse')
var bsCollapse = new bootstrap.Collapse(myCollapse, {
  toggle: false
})

I wanted to close it upon clicking on a specific element, so I implemented the following:

document.body.addEventListener("click", el => {
  if (el.target.matches("#send-search")) {
    el.preventDefault();

    // Close the element
    new bootstrap.Collapse(document.querySelector("#find-el"), {
      hide: true,
    });

    // Perform certain operations only when the element is completely closed

  }
});

The element now closes as expected...

However, I require that a particular operation be executed only after the element has been fully closed.

Answer №1

To create an event listener for the hidden.bs.collapse event...

myCollapse.addEventListener('hidden.bs.collapse', function () {
    console.log('The section is now closed!')
})

Check out this demo: https://example.com/demo123

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

What is the best way to incorporate autoplay video within the viewport?

My objective is for the video to automatically start playing when it enters the viewport, even if the play button is not clicked. It should also pause automatically when it leaves the viewport, without the need to click the pause button. <script src=& ...

Conditional styling in React class components depending on the props provided

Dealing with older versions of material-ui that cannot be updated. I'm attempting to modify the background of the Paper component based on various prop combinations without relying on the makeStyles HOC. Is this achievable? The issue seems to lie in ...

Unable to transform socket.io event into Bacon EventStream

After successfully binding the event on socket.io, the following code was executed: io = require('socket.io')() io.on 'connection', (socket) -> console.log socket.id io.listen 3000 An attempt was made to convert the socket.io ...

Retrieving Data with AJAX: Submitting Data and Retrieving Response

I need help implementing an AJAX feature for the following process: When a visitor clicks a button, I want to display a spinning/loading image using AJAX. AJAX will then access URL 1 http://www.mywebsite.com/url1.php to retrieve a random code, such a ...

Customizing hyperlink styles with JavaScript on click

Hey there! I'm experimenting with something new. I've managed to change the background color of each link after it's clicked, but now I'm facing a challenge: How can I revert the original style when another link is clicked? Here's ...

Refreshing AngularJS: How to reload Ngtable with previous data

Currently, I am utilizing NGTable to exhibit data on my webpage. The data is stored in an array and passed through the dataset parameter. However, when I dynamically modify a value in the array, the changes are accurately reflected in the table BUT if I na ...

What is the process of importing the csv-writer module?

Can we use the import statement instead of const createCSVWriter = require('csv-writer').createObjectCsvWriter; Is there a way to achieve this using import ...

The result should display the top 5 application names based on the count property found within the ImageDetails object

data = { "ImageDetails": [ { "application": "unknownApp, "count": 107757, }, { "application": "app6", "count": 1900, }, { & ...

Animation not fluid with ReactCSSTransitionGroup

Currently, I am in the process of developing an image carousel that showcases images moving smoothly from right to left. However, despite its functionality, there seems to be a slight jaggedness in the animation that I can't seem to resolve. Interesti ...

leaflet.js - Place a marker when clicked and update its position while dragging

Currently working on a small project that requires the ability to place a marker on an image-map powered by leaflet.js, and update its position if it's dragged. I was using the code below for this functionality but keep getting an error 'marker n ...

How can Node.js and Express be used to conceal Javascript code on the backend?

I'm a beginner when it comes to Node and Express. I have a query regarding how to securely hide Javascript code on the backend. Currently, I am working with Node.js and Express. My goal is to prevent users from easily accessing the code through browse ...

Prevent an endless loop in Nextjs by properly implementing conditional rendering techniques

I am working on a Next.js application that includes the following page setup: import { useState, useEffect } from 'react'; import axios from 'axios'; import { withRouter } from 'next/router'; import NotFound from '@/page ...

Display a div based on user selection using Ajax and Laravel

There are two div elements on the index page containing a datatable. By default, these two divs should be hidden. When an option is selected from the dropdown menu, the corresponding div should be displayed. The webpage is designed for searching within a ...

Why do class definitions become unavailable in Javascript due to the semantics of `require()`?

I am encountering an issue with file allocation.js which contains the definition of class Allocation {...}. In another file called test.js, I have included require('./allocation.js'), followed by a = new Allocation;, which results in a ReferenceE ...

Ways to handle errors when using navigator.clipboard.writeText

document.queryCommandSupported('copy') may not be available on all browsers. I experimented with the code below, which successfully copies the link on Firefox but fails on Opera. It displays an alert indicating that the code has been copied, yet ...

What is the method for defining a monkey patched class in a TypeScript declarations file?

Let's say there is a class called X: class X { constructor(); performAction(): void; } Now, we have another class named Y where we want to include an object of class X as a property: class Y { xProperty: X; } How do we go about defining ...

Automatically updating a database value in CodeIgniter after a countdown has expired

I am looking to automatically update a value in my MySQL database using CodeIgniter once a countdown timer reaches zero. However, I am struggling to figure out how to implement this. Here is an example: I have a database structured like this: [image lin ...

Is there a way to transfer the value of a data attribute to an element within a Bootstrap modal?

When you click on the button within any block, a modal window will open displaying data from that block. I attempted to use this Stack Overflow post as a guide, but unfortunately, it did not work for me. $(document).on("click", ".openBlogDialog", func ...

Tips for incorporating a last or recently visited feature similar to Google Docs using MongoDB

Within my database, I have two collections: users and projects. These collections share a many-to-many relationship. One user can be associated with multiple projects and one project can be assigned to multiple users. In the user object, I store an array o ...

"Why Using the Spread Operator to Copy Arrays in React does not yield the expected

While working with the state in react js, I encountered an issue where a variable named copy was being updated unexpectedly. The scenario involves copying the contents of a fake array called jObj using the spread operator and then modifying the original ar ...