implement a dropdown feature for a vertical menu with the help of Jquery

Struggling to implement a dropdown feature on a vertical navigation using Jquery. The HTML includes a nav section with 1st level list items and nested li's for second level menu items.

On clicking the 1st level li, the intention is to toggle SHOW/HIDE effect for the nested ul/li's of that specific parent li.

HTML:

<aside class="sidebar clearfix">

    <nav id="mnCat" class="primary clearfix">
            <ul>
                <li><a href="#" class="selected" data-filter="*">All</a></li>
                <li><a href="#" data-filter=".braids">Braids</a>
                    <ul>
                        <li><a href="#">Corn Rows</a></li>
                        <li><a href="#">Micro Braids</a></li>
                        <li><a href="#">Single Braids</a></li>
                    </ul>
                </li>
                <li><a href="#" data-filter=".twists">Twists</a>
                    <ul>
                        <li><a href="#">Curly</a></li>
                        <li><a href="#">Kinky</a></li>
                        <li><a href="#">Straight</a></li>
                        <li><a href="#">Wavy</a></li>
                    </ul>
                <li><a href="#" data-filter=".weaving">Weaving</a>
                    <ul>
                        <li><a href="#">Straight</a></li>
                        <li><a href="#">Wet & Wavy</a></li>
                        <li><a href="#">Curly</a></li>
                        <li><a href="#">Deep Wave</a></li>
                        <li><a href="#">Kinky</a></li>
                    </ul>                       
                </li>
                <li><a href="#" data-filter=".wigs">Wigs</a>
                    <ul>
                        <li><a href="#">Half Wigs</a></li>
                        <li><a href="#">Full Wigs</a></li>
                        <li><a href="#">Lace Fronts</a></li>
                    </ul>                    
                </li>
                <li><a href="#" data-filter=".silk">Silk Fantasy</a></li>
            </ul>
        </nav>

</aside>

JQuery:

$(document).ready(function () { 

    var $dropDown = $('nav#mnCat ul li ul');
            $dropDown.addClass("drop");

    var $trig = $('nav#mnCat ul');
            $trigger = $trig.find('a'),         
            $trigger.click(function () {
                $dropD = $dropDown.find(this),

                $dropD.css("display","block");


                });

    });

jSfiddle link: http://jsfiddle.net/SDYXy/

Answer №1

Is this what you had in mind?:

$trigger.on('click', function () {
    $(this).next('ul').slideToggle();
}

You can view the demonstration with the code above on this fiddle: http://jsfiddle.net/hearsid/SDYXy/2/

Answer №2

To address this issue, I would differentiate the class and id of the <li> elements and their corresponding child <ul> tags. By utilizing .slideToggle(), .slideUp(), and .not() in combination, I can ensure proper functionality. The following code snippet demonstrates how to achieve this:

HTML

    <nav id="mnCat" class="primary clearfix">
        <ul class="parent">
            <li><a href="#" class="selected" data-filter="*">All</a></li>
            <li class="braids"><a href="#" data-filter=".braids">Braids</a>
                <ul id="braids">
                    <li><a href="#">Corn Rows</a></li>
                    <li><a href="#">Micro Braids</a></li>
                    <li><a href="#">Single Braids</a></li>
                </ul>
            </li>
            <li class="twists"><a href="#" data-filter=".twists">Twists</a>
                <ul id="twists">
                    <li><a href="#">Curly</a></li>
                    <li><a href="#">Kinky</a></li>
                    <li><a href="#">Straight</a></li>
                    <li><a href="#">Wavy</a></li>
                </ul>
            <li class="weaving"><a href="#" data-filter=".weaving">Weaving</a>
                <ul id="weaving">
                    <li><a href="#">Straight</a></li>
                    <li><a href="#">Wet & Wavy</a></li>
                    <li><a href="#">Curly</a></li>
                    <li><a href="#">Deep Wave</a></li>
                    <li><a href="#">Kinky</a></li>
                </ul>                       
            </li>
            <li class="wigs"><a href="#" data-filter=".wigs">Wigs</a>
                <ul id="wigs">
                    <li><a href="#">Half Wigs</a></li>
                    <li><a href="#">Full Wigs</a></li>
                    <li><a href="#">Lace Fronts</a></li>
                </ul>                    
            </li>
            <li class="silk"><a href="#" data-filter=".silk">Silk Fantasy</a></li>
        </ul>
    </nav>

jQuery

$(document).ready(function () { 
    $('.parent li').click(function () {
        var showMe = $(this).attr('class');
        $('ul#'+showMe).slideToggle();
        $('.parent ul').not('#'+showMe).slideUp();
    });
});

In addition, include this CSS code snippet:

.parent ul {
    display: none;
}

This implementation ensures that clicking on a section reveals its contents with a sliding effect, while hiding the contents of other sections. Similarly, clicking on an already visible section will hide it.

For a live demonstration, you can view it here: http://jsfiddle.net/2GwJ5/1/

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

`Enter` within a container

Is there a way to use a controller in order to achieve printing test1 test2 within a <div> element... Currently, the only code I have is a simple Return "test1" & vbNewLine & "test2" However, this code displays the words on a single line ...

Conceal a division based on its numerical position

I'm having trouble figuring out how to hide multiple divs based on an index that I receive. The code snippet below hides only the first div with the id "medicalCard", but there could be more than one div with this id. document.getElementById("medical ...

Utilizing ExpressJS to refresh database query after new record insertion

I'm a beginner in using expressJS and I have a question about querying the database (mongo in this case) to retrieve all records after adding one. exports.get = function (db) { return function (req, res) { var collection = db.get('n ...

Creating a dynamic mouse trail effect using CSS and JavaScript without compromising element clickability

I'm looking to create a mouse trail that follows the cursor as it moves, made up of small icons or images that gradually fade out over time. I attempted to achieve this by overlaying a grid on top of all other elements on the page. The grid consists o ...

AngularJS and the JavaScript programming language

Struggling with opening an Excel sheet by clicking on a button? I've written the code, but encountering issues. function openFile(strFilePath) { var objExcel; //Create EXCEL object objExcel = new ActiveXObject("Excel.Applicati ...

Easily upload numerous files simultaneously with just one click of a button

This particular element, <asp:FileUpload ID="FileUploadEventosCasal" runat="server" /> is designed to upload a single file at a time when the button is clicked. I am interested in finding out how it would be possible to upload multiple files in a ...

I am unable to access Angular $scope in the HTML Template, although I can view it in the console log

I have been following some online tutorials and using the angularjs-template to start learning Angular. However, I am facing an issue where the page (html template) is not updating with the controller. It seems like there is a problem in how I've set ...

What is the best way to showcase the current element using jQuery?

I have this example: link HTML CODE: <div class="common-class"> <div class="trigger"> <span class="plus">+</span> <span class="minus">-</span> </div> <div class="show"></div> </div&g ...

When using JQuery Validation on a small screen, error messages can cause the button to shift downwards

After successfully creating an email form with jquery validation error style that appears when there's an error, I encountered a problem. When the screen width is reduced or viewed on a small screen, the error message pushes down the button. I&apos ...

Using JQuery selectors in conditional statements

In the context of my webpage, clicking on a tag filters and displays corresponding posts. I now need to handle pagination to navigate to the next set of posts. However, I am facing difficulties with the JavaScript if statement in jQuery, where I struggle ...

Utilizing CSS to apply unique styles to individual radio buttons

I have 4 radio buttons in my quiz application. I need to style only one of them, which is the correct answer option. Here's the HTML snippet that was generated: <p> <input id="SelectedAnswer" type="radio" value="5" name="SelectedAnswer"> ...

Maintain authentication state in React using express-session

Struggling to maintain API login session in my React e-commerce app. Initially logged in successfully, but facing a challenge upon page refresh as the state resets and I appear as not logged in on the client-side. However, attempting to log in again trigge ...

Strategies for Handling Errors within Observable Subscriptions in Angular

While working with code from themes written in the latest Angular versions and doing research online, I've noticed that many developers neglect error handling when it comes to subscription. My question is: When is it necessary to handle errors in an ...

Movement occurring outside the boundaries of the element

Whenever I hover over the hyperlink, it extends beyond the text of the link. Check out the jsFiddle demonstration: jsFiddle It seems like it is taking the width of the <div>, causing it to be displayed across the entire <div>. Below is the H ...

Tips for incorporating API-provided CSS values into AngularJS expressions for stylish card customization

I am in the process of creating unique Pokemon cards that pull data from an API. My question is, how can I apply specific CSS styling to each card directly from the API, similar to how I have utilized AngularJS to render the information? So far, I have su ...

Having trouble executing "npm install" following the clone from GitHub in React

After cloning a repository from GitHub, I attempted to run "npm install" but encountered the following error: Since the project is still in development, should I install or add anything else to successfully run it? ...

What is the best way to link buttons to specific drop down sections?

How can I create multiple drop down divs with different content for each button click? JSFiddle: http://jsfiddle.net/maddiwu/xe6xtfqh/ .slide { overflow-y: hidden; max-width: 100%; overflow-x: hidden; max-height: 100vw; /* approximate ...

You may encounter issues with invoking methods on a JavaScript object in Node.js after using res.send for response sending

Exploring Context and Design Overview Currently, I am utilizing a library known as Tiff.js to seamlessly load Tiff images on a designated webpage. The usage of this library extends both to the server-side and client-side functionalities. On the server end ...

I'm looking to fill multiple input boxes with the same value

I am looking to pass the same value to multiple input boxes. Currently, the script below only displays in one box but I want to display the main box result in all multiple input boxes. <script type='text/javascript' src='http://code.jque ...

The line breaks in elements occur when the combined widths add up to 100%

Is there a way to display an input and a button inline, with the input taking up 70% of the row and the button taking up 30%? I tried setting the CSS for the input to 70% and the button to 30%, but they keep breaking onto separate lines. How can I solve ...