Only one submenu can be opened at a time & click to close

I am encountering an issue with my list and submenus. When I click on List 1, submenu 1 opens but if I then click on List 2, submenu 2 opens while submenu 1 remains open. However, when I click on List 2 again, instead of just closing submenu 2, it toggles open and closed repeatedly.

Any assistance with this matter would be greatly appreciated.

Here is the jsfiddle link for reference

    $(document).ready(function () {
        $('li a').on('click', function(){
        $('.square').slideUp();
            $(this).siblings().slideToggle();
        });
    });
    li {padding:20px; border: solid 1px;margin:5px;}
    
    .square {
      display: none;
      border: 1px solid red;
    }
    .active {
      display: block;
      border: 1px solid red;
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
        <li>
            <a class="test">Hover List1</a>
            <div class="square">
            Square 1
            </div>
        </li>
        <li>
            <a>Hover List2</a>
            <div class="square">
            Square 2
            </div>
        </li>
        <li>
            <a>Hover List3</a>
            <div class="square">
            Square 3
            </div>
        </li>
        <li>
            <a>Hover List4</a>
            <div class="square">
            Square 4
            </div>
        </li>
    </ul>

Answer №1

To achieve the desired effect, use the slideUp() function on the div elements that are siblings of the parent:

$(this).parent().siblings().find('.square').slideUp();

$(document).ready(function () {
    $('li a').on('click', function(){
   
         $(this).parent().siblings().find('.square').slideUp();

    
        $(this).siblings().slideToggle();
    });
});
li {padding:20px; border: solid 1px;margin:5px;}

.square {
  display: none;
  border: 1px solid red;
}
.active {
  display: block;
  border: 1px solid red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
    <li>
        <a class="test">Hover List1</a>
        <div class="square">
        Square 1
        </div>
    </li>
    <li>
        <a>Hover List2</a>
        <div class="square">
        Square 2
        </div>
    </li>
    <li>
        <a>Hover List3</a>
        <div class="square">
        Square 3
        </div>
    </li>
    <li>
        <a>Hover List4</a>
        <div class="square">
        Square 4
        </div>
    </li>
</ul>

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

Can a linked checkbox be created?

Is it possible to create a switch type button that automatically redirects to a webpage when turned on by clicking a checkbox? I'm working on implementing this feature and would like to know if it's feasible. ...

Deciphering HTML with AngularJS

We are currently working with AngularJS version 1.5.6 and facing an issue with HTML characters not being displayed correctly in our text. Despite trying various solutions, we have been unsuccessful in resolving this issue. We have explored numerous discuss ...

Unattended HTML and head tags found unexpectedly

As a new programmer tackling my first RoR site with an integrated bootstrap theme, I've encountered some issues with the HTML code that are causing problems with image sharing on Facebook. The error message from the debugger indicates that there are m ...

The functionality of switching between two layouts using a JavaScript button is currently not functioning

My concept involves implementing a switch that alternates between displaying a bootstrap carousel and cards. Essentially, one of them will be visible when the button is pressed while the other remains invisible. Initially, I am attempting to hide my existi ...

The hierarchical structure in the DOM that mirrors an HTML table

While working on code to navigate the DOM for a table, I encountered an unexpected surprise. The DOM (specifically in FF) did not align with my initial expectations. Upon investigation, it appears that FF has automatically inserted a tbody along with sever ...

My Fullcalendar is displaying events for the upcoming month. How can I resolve this issue?

This is the start date for my event in the full calendar: start: new Date('2016', '02', '07', 00, 30) However, when loading the calendar, this event is displaying on March 7, 2016. How can I resolve this issue? ...

Concealed scrollbar only visible upon resizing the page

For my personal project, I was in need of a custom scrollbar plugin with basic inertia effects and custom images. After some research, I decided to go with mCustomScrollbar. The documentation provided was quite clear, and implementing the script was troub ...

Tips for adjusting the size of a background image using zoom

Currently, I am in the process of creating my first website. At this stage, it consists of a background image, an image, and a non-functional password input section. Everything seems to be going well so far. The background image fits perfectly on the scree ...

Pressing the button will allow you to select and copy the text within the

I am looking to incorporate a mock-chat feature into my website. The concept is to type something on the website, then click a button next to it which will move the text to a frame above. I attempted this using a textarea and even found a code for selectin ...

Guide to turning off the Facebook iframe page plugin

I am facing a challenge with embedding a facebook iframe on my website. I want to disable it once the screen width reaches a specific point, but I am struggling to make the iframe disappear properly. Here is how I have attempted to implement this: window.o ...

The candy stripes on a loading bar zebra assist in creating a unique and

I'm in the process of creating my portfolio and I'd like to incorporate unique animated loading bars, such as vertical or horizontal candy stripes, to showcase my coding skills. Can anyone suggest a specific programming language or tool that woul ...

Having trouble displaying a gray background/overlay in a Twitter Bootstrap modal design

Library version: Jquery 1.11.0 Framework version: Bootstrap 3.1.1 CodePen link: https://codepen.io/amitshahc/pen/r6ueupfj/1/ HTML snippet: <!-- Modal --> <div class="modal fade" id="az-progress" tabindex="-1" role="dialog" aria-labelledby= ...

Eliminating unnecessary CSS from the codebase of a website

Currently, I am making adjustments to a website template that I downloaded for free online. I have noticed that even if I delete a div from the code, the corresponding CSS styles remain in one or more files. Is there any tool available that can automatic ...

Tips for creating a blur effect on all options except for the selected 2 out of 4 using jQuery

My goal is to choose 3 options from a list and once 3 options are selected, all other options will be blurred to prevent further selection. Please review the code snippet I have provided below. Link to File <!DOCTYPE html> <html> <head> ...

Leverage the power of jQuery's .filter() method to pinpoint and target specific text

HTML: <p class="greeting"> hello, my name is kevin. what's yours? </p> jQuery: $("p.greeting").filter(function (){ return $this.text() === "my name is"; }).css("background", "green"); I am attempting to find and highlight the phra ...

Is it time to switch out HTML tables for Divs?

I am looking for advice on creating a consistent header for all of my website pages. Currently, I use a div element to wrap a table that contains one row with three cells. These cells hold three images: one in the top-left corner, one in the top-center, a ...

Saving JSON data as a file on server

Currently, I am running a localhost website on my Raspberry Pi using Apache and I am seeking advice on how to export a JSON string to a file on the Raspberry Pi itself. While I do know how to export a file, I am unsure of how to send it to the Raspberry Pi ...

jQuery functions smoothly on Firefox, but encountering issues on Chrome

While it may seem like a repetitive inquiry, I have thoroughly researched similar questions and have not found a suitable solution. The server is returning the following response through a standard ajax call: <form id="ctl00" name="ctl00" method="post ...

Parsing through JSON data to showcase numerous rows

I have been struggling with JSON data for the past few days, as I haven't had much experience working with it. Part of my application that utilizes jQuery data tables displays information about available funds on a service contract. When diving deepe ...

I am struggling to make php redirect work using onclick() function

My PHP button is not redirecting properly. Assuming the page destination is correct, is there anything else that could be causing this issue? echo "<button id=\"create\" onclick=\"location.href('/team/teams.php?op=create');&bso ...