Learn how to hide a div element by applying the CSS property "display: none" on a click event, all while referencing the div's class

Hey there! I need some help with my bootstrap dropdown script. I'm trying to add a "display: none" style to my div using JavaScript. Here's the code I've been working with:


var modal = document.getElementById('black');

window.onclick = function(event) {
  if (event.target == modal) {
    modal.style.display = "none";
  }
}

$('.drp').on('hidden.bs.dropdown', function () {
  modal.style.display = "none";
});

$('.drp').on('shown.bs.dropdown', function () {
  modal.style.display = "block";
});

$('li.dropdown.mega-dropdown a').on('click', function (event) {
  $(this).parent().toggleClass("open");
  modal.style.display = "block";
});

$('body').on('click', function (e) {
  if (!$('li.dropdown.mega-dropdown').is(e.target) && $('li.dropdown.mega-dropdown').has(e.target).length === 0 && $('.open').has(e.target).length === 0)  {
    $('li.dropdown.mega-dropdown').removeClass('open');
  }
});

I'm looking to trigger (modal.style.display = "block";) on the click event of (li.dropdown.mega-dropdown).

Answer №1

I completed this task on my own, but I realized that I was missing an if condition in my script. Here is the correct solution: [DEMO][1]

[1]: https://jsfiddle.net/dami_012/L7yz6v3j/3/

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

Building a Dynamic Web Widget with the Perfect Blend of HTML, JS,

While developing a javascript-based web widget, I am aiming to steer clear of using iframes and avoid relying on the page's CSS. It is infeasible for me to utilize custom CSS, as it defeats the purpose, and iframe integration would not present a user- ...

Tips on retrieving a record within a while loop

Product Table : id | mid | pid | owgh | nwgh | 1 3 12 1.5 0.6 2 3 12 1.5 0.3 3 3 14 0.6 0.4 4 3 15 1.2 1.1 5 4 16 1.5 1.0 6 4 17 2.4 1.2 7 3 19 3.0 1.4 8 ...

Encountered an issue retrieving audio during recording with Recorder.js

I've come across a scenario where I'm utilizing the Recorder.js Demo for recording audio. Interestingly, it works perfectly on Linux but encounters an issue when used on Windows. A pop-up alert stating "Error getting audio" shows up. Here's ...

What is the most effective method for dealing with the error message "cannot access property * of undefined"?

This particular error seems to be quite common in the world of javascript development. When trying to read the property join of undefined, an error occurs. What is considered to be the most effective method for resolving this issue? Here are a few tec ...

The patch method is failing to upload using axios

In my project using Laravel 5.8 and Vue.js 2, I have encountered a problem with the .vue file: let formData = new FormData(); formData.append('name', this.name); formData.append('image',this.image) formData.a ...

Manipulating HTML attributes with Jquery's attr() method results in returning [object Object]

Despite reading numerous articles and questions, I have yet to find a solution. My PHP page is designed to update an easypiechart using AJAX with database values checked every X minutes. For demonstration purposes, I have set the update interval to 10 seco ...

What is the best way to arrange three photos in columns of four each below one image spanning across all twelve columns?

Struggling with the bootstrap 5 grid? I'm trying to showcase one full image with a col-12 and 600px width, along with three images, each 200px wide with a col-4. The goal is to have one image on top and three beneath it with g-3 spacing between them. ...

The jquery click event is not working as expected

Hey there, I've been working on creating a dropdown menu that can be activated by clicking for better usability on touch screens. $('a.dropsmall2').click(function() { $(this).next('ul').slideToggle(500,'easeInOutQuad'); ...

Unable to access elements located underneath the div

I'm working on creating a simple menu using CSS and jQuery. Everything seems to be functioning correctly, but I've noticed that when I open the menu, I am unable to click on any elements below it. Check out the menu here <header> <nav ...

Issue with Sliding Transition in React Material UI Drawer Component

I developed a custom drawer component for my React application const CustomSidebar = () => { return ( <Drawer open={drawerOpen} onClose={() => setDrawerOpen(false)} > <Box> <Navigator / ...

Getting the result from HTML5 FileReader: How does it work?

Dealing with the asynchronous nature of JS FileReader can be challenging. I need to retrieve the result after reading a file and work with that data, but I don't know when the result will be ready. How can I handle this situation effectively? $(docum ...

Disable onclick action for programmatically created buttons

I'm facing an issue with the code I'm using to delete messages on my website. The problem is that while newly added messages are being dynamically loaded, the delete button (which links to a message deletion function) does not seem to be working. ...

Having difficulty with express.index when trying to send a JSON object

Express is my tool of choice for creating a simple web page. The code in my index.js file looks like this: exports.index = function(req, res){ res.render( 'index', { title: 'Expressssss', Tin: va ...

The nested router fails to provide the next callback containing the value of 'route'

While developing a middleware handler, I ran into an interesting issue where passing the route string as an argument for the next callback resulted in a value of null. Here's an example to illustrate this: var express = require('express') ...

Dealing with website links in Next.js and Chakra-UI: Tips and Tricks

When incorporating react linkify directly with chakra-ui components such as Text, the links cannot be managed. Issue Example import Linkify from 'react-linkify'; import {Box, Text} from '@chakra-ui/react'; export default function Usag ...

Switch the hue of "BRANDS"

I have a website at the following link: My goal is to change the text color of "Nike" to white. If you decide to alter this text and modify the product name, please let me know so we can discuss it further. a { color: #F7F6F6; text-decoration: non ...

Calculating the initial element in a multi-dimensional array using Javascript

I have the following code snippet to retrieve the ID and username of a unique set of authors. let authorsList = await Poem.find({ 'author.id': {$nin: community.busAdmins}}).where('communities').equals(community._id).populate('a ...

A redirect occurs upon clicking within the <a> tag, even with the use of $event.stopPropagation

Original Template: <a href="https://example.com/" target="_blank"> Documentation <i ng-click="$ctrl.hideDocumentation($event)" class="fa fa-times remove"></i> </a> Controller Function: function preventRedirection($event) { $e ...

Voting Dynamics in Django: Ordering by Popular Vote

There is a lot of existing documentation on this topic, but so far, none of the solutions have worked for me. As the title suggests, my goal is to sort a set of objects using Django Voting based on the number of votes they have (from high to low). I have a ...

Unlock a multitude of outcomes with Sequelize

Is there a way to retrieve multiple results from Sequelize and store them in an array? For example, I want to fetch all the values in the name field from the test table and display them in the console. This is what I tried: test.findAll().them(function(re ...