Close the Bootstrap 3.1.1 collapse feature by clicking outside of it

I'm currently working on a Twitter Bootstrap 3.1.1 app that features a nested Bootstrap structure.

<ul class="goods-categories goods-list">
  <div id="point" data-toggle="collapse" data-target="#all" class="point blue m-top">All products</div>
  <div id="all" class="collapse">
    <li><a href="/products?category_id=11">Paper Products</a></li>
    <li><a href="/products?category_id=14">Notepads, Dispensers</a></li>
    <li><a href="/products?category_id=13">Notebooks, Journals</a></li>
  </div>
</ul>

I am trying to figure out how to implement a feature where the collapse toggles closed when a user clicks outside of the #all and its children. Any ideas on how to achieve this?

Answer №1

To manually show/hide collapsible items, you can utilize the functions provided by bootstrap collapse. For your situation, the following code snippet should prove to be effective:

$('body *').not('.goods-categories.goods-list, .goods-categories.goods-list *').click(function() {
  $('#all').collapse('hide');
});

The code snippet above attempts to attach a click event to the body element excluding the specified list and its descendants. Upon clicking anywhere outside of the list, the collapsible item is hidden.

Note: The code has not been tested, but with slight modifications, it should be functional.

Answer №2

$(document).click(function (e)
{
    var box = $("#container");
    if (!box.is(e.target) && box.has(e.target).length === 0)
    {
        box.removeClass('active');
    }
}); 

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

Arrange a line of images in the center

I've been working on aligning images in a row and centering them using flex, but I'm having trouble figuring it out. I managed to center all the images with flex and justify-content center, but setting flex-direction: row on the container doesn&a ...

Position the text in the center of a graph within a grid cell by utilizing dash and plotly

Is there a way to center text over a graph within a cell of a grid table using dash and plotly? I'm struggling with the alignment currently, as the graph is not centered while the text appears centered in the view but not in the cell itself. You can ...

Tips for preventing cursor tooltips from going off the screen

I've been working on creating a popup box that displays text positioned away from an element when the cursor hovers over it. Check out a simplified demo here. Currently, the popup box collapses when it gets close to the bounding box. I'd like it ...

Error detected at line 15, column 2: ` unable to open socket: /tmp/mysql.sock'`

Hello everyone at Stack Overflow! After starting up my server, I encountered the following error: Started GET "/demo/hello" for 127.0.0.1 at Tue Jan 18 16:42:42 -0500 2011 ArgumentError (syntax error on line 15, col 2: ` socket: /tmp/mysql.sock'): ...

What is the best way to stack a canvas box on top of another canvas box?

My goal is to have two canvas squares stacked on top of each other. While I am familiar with drawing on canvas, I need assistance in placing one canvas on top of another. Despite my attempts at setting the position, I have not been successful. <canvas ...

The data is being uploaded, but it is not being stored in the database

I am struggling with saving HTML content from a contenteditable DIV into the database. Although the data is being posted successfully, when it comes to inserting into the database, it turns out to be nil. Here is the log: Started POST "/pages" for 127.0. ...

My dropdown does not appear to be moving up properly in responsive mode

<!DOCTYPE html> <html> <?php include 'includes/head.php'; ?> <body> <?php include 'includes/header.php'; ?> head.php:- <head> <meta name="viewport" content="width=device-width, initial-scale=1"& ...

Enhance User Experience in IE11 by Providing Font Size Customization with Angular (Using CSS Variables)

I am in the process of developing an Angular application. One of the requirements is to allow the user to choose between small, medium, or large font sizes (with medium being the default) and adjust the font size across the entire webpage accordingly. Wh ...

Troublesome tab key function in FireFox causing navigation issues

I'm facing an issue with the tab key focus on links in FireFox. Strangely, it's working fine in Chrome but in FireFox, it keeps looping within one element. For better understanding, I have created a demo showcasing this behavior specifically in ...

The spacing between elements in Flexbox is too excessive, creating an overly wide gap

How can I maintain a fixed 40 pixel gap between columns in flexbox without it increasing as the screen width widens? (I apologize for the brevity of this description, but there are no additional details to provide at this time) .browser-box { max-wid ...

Advanced data synchronization with Angular 7's bidirectional data binding feature

One input box allows users to enter a number, and I would like to adjust the width of the background color based on that number. The box itself will have a fixed width, but I am looking to dynamically change the width of the background color according to ...

Implement a vertical background sprite animation using jQuery along with a mask

Is it possible to create a jQuery background vertical animation that smoothly transitions the sprite position, giving the appearance of fading into the next position? Currently, my code (view demo jsfiddle link below) moves the sprite to the correct backgr ...

MUI Typography - Text turns white when hovered over

I've created a customized component that turns green on hover. Here's the code for it: const ProjectAdd= styled.div` background-color: white; list-style-type: none; list-style: none; height: 100%; width: 10%; height: 100%; border-ra ...

How to Style Background with CSS on the Server-Side? (Including Links and Hover Effects)

I'm facing a dilemma in choosing between an ASP.NET control and an HTML element for creating a button with a CSS-defined background that changes on hover... 1. Initially, it seems the ASP.NET ImageButton control is not suitable for this purpose becau ...

The Model Viewer module is unable to load the three-dimensional model

Attempting to incorporate a 3D model into my website using the modelviewer library, but encountering difficulties in loading the file as shown below: Just to note, I am utilizing github pages with a custom domain from godaddy which may be contributing to ...

Having trouble compiling your Vue project and running into the "No mixed spaces and tabs" error?

Below are the details with an error: Error Details: Failed to compile. ./src/components/Header.vue Module Error (from ./node_modules/eslint-loader/index.js): H:\project\VueProjects\stock-trader\src\components\Header.vue 27: ...

The BorderWidth property is set to 2px, however, it is not visible on the

Struggling with adding a border to a div in my app built with react and material-ui. Here is the snippet of code I've been working with: <div className={classes.search}> ... </div> search: { ... borderWidth: '2px', borde ...

"Enhance your website with a dynamic hover effect and striking letter spacing in

In the div below, there is a span with the following code: <div class="mission-button"><span class="mission">view our mission</span></div> Accompanied by this CSS: .mission-button::before { content:'&ap ...

HTML - Customizing container with background color and ensuring minimum height of 100%

After creating a dynamic page using Bootstrap, I managed to add a sticky nav bar and footer. The navbar, footer, and page content are all in black color. I want the main content area to be white with black sides matching the background. Currently, I ca ...

Troubleshooting problem with CSS background image

I'm looking to make my webpage more dynamic and interactive. One issue I have is that the background image of the body element stretches when new elements are added, which is not the desired behavior. Any suggestions on how I can fix this? <styl ...