Ways to collapse a div by clicking outside of it?

I have been working on implementing a dynamic 'drawer' feature where clicking on any link in a navigation bar opens the drawer and displays relevant content based on the clicked link. However, I have hit a roadblock in my progress despite thorough research.

I am struggling to find a solution that integrates seamlessly with my current setup. My goal is to make the drawer fade out when clicked anywhere else on the page. Can someone provide insights on how I can achieve this within my existing framework?

Thank you for your assistance!

$('.filters-toolbar__drop-down:gt(0)').hide();
$('.filters-toolbar__drop-down-wrapper').hide();

$('.filters-toolbar').on('click', 'a', function() {
  $('.current').not($(this).closest('li').addClass('current')).removeClass('current');
  var $this = $(this);
  $('.filters-toolbar__drop-down-wrapper').fadeIn('slow');
  $('body').addClass('filter-open');

  // fade out all open subcontents
  var visibleElements = $('.filters-toolbar__drop-down:visible');
  if (visibleElements.length <= 0) {
    $('.filters-toolbar__drop-down[id=' + $this.data('id') + ']').fadeIn('slow');
  } else {
    visibleElements.fadeOut('slow', function() {
      $('.filters-toolbar__drop-down[id=' + $this.data('id') + ']').fadeIn('slow');
    });
  }
});
.filters-toolbar-wrapper {
  margin-bottom: 0;
  width: 100%;
}

.filters-toolbar__item {
  padding: 0 50px;
   display: inline-block;
}

.filters-toolbar__drop-down-wrapper {
  background-color: grey;
  border-bottom: 1px solid #000;
  position: relative;
  top: 0;
  left: 0;
  width: 100%;
  height: 200px;
}

.filters-toolbar__drop-down {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="filters-toolbar-wrapper">
  <div class="filters-toolbar">

    <div class="filters-toolbar__item">
      <a href="#" data-id="filter1">Type</a>
    </div>
    
    <div class="filters-toolbar__item">
      <a href="#" data-id="filter2">Color</a>
    </div>

    <div class="filters-toolbar__item">
      <a href="#" data-id="filter3">Sorty by</a>
    </div>
    
  </div>
</div>

<div class="filters-toolbar__drop-down-wrapper">
  <div id="filter1" class="filters-toolbar__drop-down by-type">
    Type Dropdown Content
  </div>
  <div id="filter2" class="filters-toolbar__drop-down by-color">
    Color Dropdown Content
  </div>
  <div id="filter3" class="filters-toolbar__drop-down sort-by">
    Sort Dropdown Content
  </div>
</div>

Answer №1

To detect when the document or window is clicked, you can create an event listener and then compare the attributes of the drawer element. If the attributes do not match, simply close the drawer.

document.addEventListener('click', function(event) {
// Check if the clicked element has the id 'drawer'
if (event.target.id != 'drawer') {
  `$("#drawer").fadeOut();`
}
});

This listener should be added within your link click function and can be removed after calling the `fadeOut` method.

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

Creating a dynamic shift in background color

Is it possible to use jQuery to slowly change the color of diagonal lines in a background styled with CSS, while also adding a fading effect? I have created a fiddle with the necessary CSS to display a static background. You can view it here: http://jsfid ...

Tips for automatically opening a specific section of a collapsible menu upon page load using a predetermined value

JSFiddle: http://jsfiddle.net/5qweu58f/4/ HTML (generated using XSLT): <div id="dvExpProvHolder" class="hidOverflow innerDivCenter"> <ul class="uSPStyle" id="uSPStyle"> <li class="setRelative"> <a class="tfLin ...

Invoke a jQuery function in the parent page using regular JavaScript from an iframe

I have successfully created an iframe using regular javascript. Inside the iframe is a Jquery function along with JQuery itself and it functions correctly within the iframe. However, I am now looking to execute this function from the parent page instead of ...

Say goodbye to my HTML5 creations

I am currently working on an HTML5 grid project that involves implementing a rectangle select tool for use within the grid. Everything is going smoothly, except for the fact that when I attempt to use the rectangular select tool, the grid disappears from t ...

Border color options for select boxes

Currently, I am working on my first Django-bootstrap project and dealing with a select box. I am trying to customize it so that when clicked, the border of the select box and its options turns yellow, which is working well. However, there is an additional ...

React app (storybook) experiencing loading issues with @font-face

I am struggling to load custom fonts from a local directory. Can someone provide assistance? Below is the code I am currently using: @font-face { font-family: 'My Custom Font'; src: url('./fonts/MyCustomFont.eot'); src: url(&apo ...

Having trouble getting a background image with tint to display properly in Bootstrap

I applied a sleek black transparent gradient to my body background picture, but as soon as I integrated bootstrap into the HTML code, the effect ceased to function. body { background: linear-gradient( rgba(0, 0, 0, 0.5), rgba ...

Upon submission of user-entered data, save it in an HTML table by creating an array variable for each column of the table

There is an HTML table containing multiple rows where users can input details such as Sprint, Role, Project, and Comments. The remaining fields, SID and Project Code, are fetched from the backend when the user clicks on the getDetails button. Users have th ...

I always love playing around with CSS to create unique effects with my images, constantly changing their size through shrinking

I'm working on coding 3 different boxes. Each box consists of an image and a title in the first column, followed by a paragraph, and a link at the bottom. The boxes themselves are not editable, only the content inside them can be changed. The challeng ...

Modify CSS image according to the user interface language in asp.net

Is there a way to dynamically change the image based on different cultures in my ASP.NET webpage? I have successfully been able to switch strings using a resource file, but I am unsure how to handle images. Currently, I have an A tag with a specific clas ...

What can you tell me about creating a dropdown tree combo box using JavaScript?

Upon reviewing my work, I noticed that the sequence of the drop-down boxes has been inadvertently reversed. This is not aligned with the desired outcome. How can I utilize a method to ensure that the order of the drop-down boxes is correct? However, this ...

Attempting to display a larger version of an image sourced from miniature versions fetched with the assistance of PHP and

I'm dealing with the challenge of displaying thumbnails fetched from a database. PHP is successfully interacting with and presenting my thumbnails. I'm currently faced with the issue of passing the id from the database to the imageID in my JavaSc ...

Split the input string into individual characters for each entry

As a beginner in Angular, I am currently exploring ways to split a single input field into separate inputs for each word. I attempted to achieve this using jQuery but struggled and also learned that mixing jQuery with Angular is discouraged. Here's th ...

Exploring child elements with jQuery

I am facing an issue with resetting a series of select boxes within a table. Here is the structure: <tr> <td><select></select></td> <td><select></select></td> <td><select></select>< ...

Assisting in AJAX with Rails API

My Rails website functions normally, but on certain pages I require the use of AJAX. I have successfully implemented AJAX Javascript code using jQuery without the assistance of rails helpers by manually writing corresponding paths in strings. However, I ...

The class container-fluid from Bootstrap does not seem to be functioning properly within a Django environment

<div class="container-fluid"> <div class="row"> <img class="graph" src="{% static 'images/graph.png' %}"/> </div> </div> Hey there! I'm trying to make the graph.png static image fill the entire width of ...

Filtering HTML tables with text and image search functionality

I have an HTML table that displays text and icons. I've successfully implemented image filtering and now I'm looking to add text search functionality as well. <div id="usertable"> <div class="choicebox"> <div id="choicebox"> ...

I created an image that can be clicked on, but unfortunately it only functions properly on the

I am currently working on creating an image that can be clicked to cycle through different images all within the same frame. While I have managed to get it to work, I am facing a limitation where it only responds to one click. count = 1; function myF ...

Tips on creating adaptable images for mobile viewing

My coding conundrum involves the use of two columns - one for an image and the other for a description of that image. However, when viewing my site on mobile devices, the image is cut off at only half its height. Adjusting both columns to col-sm-6 results ...

Executing a jQuery function once all get requests have been successfully completed

Hey there! I have a question about my website's home page. I want to create 4 carousels using the .get method when the page loads. I'm curious about how I can call a JavaScript function after each carousel has been successfully loaded. The number ...