Using Jquery to attach an event to a particular div which is part of a group of divs sharing

I am trying to implement a mouseup event on a series of divs that, when clicked, will reveal a child div ('menu'). All the parent divs have the same class. Here is an example:

<div class="container">
  <div class="menu"><p>Text</p></div>
</div>
<div class="container">
  <div class="menu"><p>Text</p></div>
</div>
etc...

However, I want the event to only trigger when the specific 'container' is clicked. When I click on another 'container', I want the previously opened 'menu' to hide while revealing the new one. My current solution involves creating variables for each container and giving them unique classes.

  $(document).mouseup (function (e){

   var buttonOne = $(".containerOne");
   var buttonTwo = $(".containerTwo");
   var menuOne = $(".containerOne").find(".menu");
   var menuTwo = $(".containerTwo").find(".menu");

   if(buttonOne.is(e.target)){
     menuOne.toggle(100);
     menuTwo.hide();

   }

   else if(buttonTwo.is(e.target)){
     menuTwo.toggle(100);
     menuOne.hide();
   }

   else {
    $(".menu").hide();
   }
 });

Check out this JSFiddle link for a quick demo.

Doing this for multiple containers results in long lines of code, and I believe there must be a simpler way to achieve this. Sorry if my explanation is unclear, it has been a tiring day!

Answer ā„–1

Include a new class in the containerxxx section and then implement a basic click handler

<div class="containerOne container">
    <div class="menu">
        <p>Text</p>
    </div>
</div>
<div class="containerTwo container">
    <div class="menu">
        <p>Text</p>
    </div>
</div>

subsequently

var $menus = $('.container .menu');
$('.container').mouseup(function (e) {
    var $menu = $(this).find('.menu').toggle();
    $menus.not($menu).hide()
});

Demonstration: Fiddle

Answer ā„–2

What do you think of this solution?

$(".wrapper").on("click", function() {
    var selected = $(".dropdown", this).toggle(150);
    $(".dropdown").not(selected).hide();
});

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

Continuously executing a series of JQuery functions or iterating through them repeatedly

Struggling with a jQuery animation loop issue that has me stuck. I need the animation to repeat continuously, but it's not working despite trying setInterval. Can anyone assist? Here's the fiddle link: https://jsfiddle.net/8v5feL9u/ $(document). ...

Utilize Angular to transform a standard text string within a JSON object into a properly formatted URL

Welcome to my first Stack Overflow question! I usually find the answers I need on my own, but this time I could use some guidance as I delve into Angular. I have a directive that pulls the name and URL from a JSON object and generates HTML with the name a ...

"Exploring the versatility of using variables in jquery's .css

Iā€™m facing an issue where I need the rotation of a div to be based on the value stored in "anVar." This is what I currently have: function something() { $('.class').css('-webkit-transform:rotate('anVar'deg)') } The desi ...

Is it possible to eliminate the background color by double clicking on a row within an HTML table?

I am currently working on an HTML table with jQuery functionality that allows for highlighting a row when selected, and double-clicking to remove the highlight. Additionally, I would like to ensure that only one row is selected at a time. The code below s ...

Utilize ng-bootstrap in an Angular CLI project by integrating it with npm commands

I've been attempting to set up a project using Angular CLI with ng-bootstrap, but I'm having trouble getting the style to work properly. Here are the exact steps I followed (as outlined in the get-started page): Create a new project using `ng n ...

Encase every picture within a div and add a numerical label in front of it

I'm working on a function that will wrap each image in a div and add a span label for each image. Here is the code I have written so far: $('#carousel img').each(function(index) { $(this).wrap('<div class="image"></div>& ...

An unspecified number of Ajax requests being made within a loop

Here is the dilemma I'm facing, despite trying different recommendations from StackOverflow: Situation: I am utilizing the Gitlab API to display all the "issues" in the bug tracker of a specific project on the system. Challenges: Everything wor ...

What is the best way to create a time delay between two consecutive desktop screenshot captures?

screenshot-desktop is a unique npm API that captures desktop screenshots and saves them upon request. However, I encounter the need to call the function three times with a 5-second delay between each call. Since this API works on promises, the calls are e ...

How can I display a "loading..." message as a temporary placeholder while waiting for my Apexcharts to load?

I've spent a day trying to solve this issue but couldn't find a solution. Any help would be greatly appreciated. Recently, I was working on creating a cryptocurrency tracker in React. I successfully built a table that displays multiple currencie ...

Encountering an error while attempting to launch an AngularJS application on Node.js? Let's

Currently, I am in the process of learning Angular. Whenever I attempt to run my code, an error message pops up: > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1f7a737a7c6b6d70715f2b312f3115">[email protected]< ...

``The Dynamic Checkbox Selection Process using Ajax, Jquery, and PHP

I'm a bit confused about the next steps... <input id="" type="checkbox" name="" onClick="" /> <td> <h4>PDF Document</h4> <p> Do you have your own menu or flyer? You can add it to your App </p> ...

Troubleshooting problems with callback functionality in conjunction with Expressjs, JWT authentication, and Angular

For my current project, I am implementing authentication using JWT with Expressjs and Angularjs. However, I now need to integrate node-dbox to access Dropbox files of my users. The issue arises when trying to store the token received from Dropbox for user ...

Sharing images on Facebook using Data URI

In my current project, I have developed an application that allows users to create designs interactively on an HTML5 canvas. My goal is to enable them to share the canvas image on Facebook. Initially, I intended to use a Data URI by creating a dynamic pag ...

What is the best way to incorporate several functions within a resize function?

Is it possible to incorporate multiple functions within the windows.width resize function? I have been experimenting with some code, but I would like to restrict its usage to tablet and mobile devices only, excluding laptops and PCs. Any suggestions on h ...

Utilizing Jquery: Extracting the value of a child element upon clicking the encompassing parent div

I've created a table-like structure using divs instead of tables (because I strongly dislike tables). However, I'm facing an issue. I want to be able to click on one of the div elements and extract the values of its child elements. <div cla ...

I can't seem to figure out what's causing this error to pop up in my coding setup (Tailwind + Next.js). It

I've been struggling with this problem for a week now and despite my efforts, I haven't been able to find a solution yet. However, I have made some progress in narrowing down the issue. The problem arises when I try to execute the yarn build comm ...

Error message: "Angular.js encountered a typeError stating that V2.example is not a function"

Having recently started learning Angular.js, I came across a tutorial that was created about a year ago. In this tutorial, I am attempting to implement a search feature that takes user input and searches for it on Github.com. Below is the HTML code snippet ...

Filter records using jQuery to narrow down results within a specific range of values

I am currently attempting to create a jQuery-based search engine using an XML file. I am specifically looking to retrieve properties with a price range between x and y. Despite utilizing the filter function, it seems that there may be an error in my code o ...

What could be causing my Wikipedia opensearch AJAX request to not return successfully?

I've been attempting various methods to no avail when trying to execute the success block. The ajax request keeps returning an error despite having the correct URL. My current error message reads "undefined". Any suggestions on alternative approaches ...

Arranging items into an array?

I have a query. Let me start by posting the HTML code, even though I know using tables for design is not recommended. However, in this case, it's only for educational purposes. <table id="placeholder"> <tr> <td><img src="img/ ...