Tips for maintaining an open side menu

I'm having trouble keeping the side menu open when a user clicks on one of the child menu items. Ideally, I want section 1 to display section 2 content when hovered over, and keep both sections open even when section 2 is clicked. The only time they should close is if section 4, a child of section 3, is clicked.

The issue I'm facing is that I can't seem to maintain the open state when a child menu item is clicked.

Below is my HTML:

<div id="second-menu" class="collapse navbar-collapse menu_two">
    <ul class="nav navbar-nav inside-nav">
        <li class="sub_menu">
            <a href="#">Section 1</a>
            <ul class="sidenav_wrapper">
                <li class="sidenav_item sidenavlast">
                    <a href="#">Section 2</a>
                </li>
            </ul>
        </li>
        <li class="active_sub_menu">
            <a href="#">Section 3</a>
            <ul class="sidenav_wrapper">
                <li class="sidenav_item">
                    <a href="#">Section 4</a>
                </li>
                <li class="sidenav_item">
                    <a href="#">Section 5</a>
                </li>
                <li class="sidenav_item">
                    <a href="#">Section 6</a>
                </li>
                <li class="sidenav_item sidenavlast">
                    <a href="#">Section 7</a>
                </li>
            </ul>
        </li>
        <li class="sub_menu">
            <a href="#">Section 8</a>
            <ul class="sidenav_wrapper">
                <li class="sidenav_item">
                    <a href="#">Section 9</a>
                </li>
                <li class="sidenav_item">
                    <a href="#">Section 10</a>
                </li>
                <li class="sidenav_item sidenavlast">
                    <a href="#">Section 11</a>
                </li>
            </ul>
        </li>
    </ul>
</div>

This is my JavaScript code:

$('ul.navbar-nav li').hover(
    function () {
        $(this).find('.sidenav_wrapper:first').css('display','block');
    },
    function () {
        $(this).find('.sidenav_wrapper:first').css('display','none');  
    }
);

And here is my CSS:

.menu_two {
    border-left: 16px solid #dfdfdf;
    padding-left: 0;
}

.sidenav_wrapper {
    background: none repeat scroll 0 0 #e7ecf5;
    border-left: 6px solid #8fb8e6;
    display: none;
    padding: 0;
}

You can view a live demo on JSFiddle here.

Answer №1

Elaboration

In order to enhance your fiddle, I have incorporated a jQuery .click() function. This particular function is designed to append an .open class to the existing .sidenav_wrapper, and also remove the .open class from any other .sidenav_wrapper which may be selected when a link within a .sidenav_wrapper is clicked. By utilizing e.preventDefault(), the default hyperlink action is prevented, thus preventing redirection to another URL.

CSS

.open {
    display: block !important;
}

jQuery

$('.sidenav_wrapper').click(function(e) {
    e.preventDefault();
    $('.sidenav_wrapper').removeClass('open');
    $(this).addClass('open');
});

To see this in action, check out the updated jsFiddle

Answer №2

Success! After implementing a PHP loop to designate a menu item as active and generating an active class, I was able to display the desired content in block format.

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

Click event for jQuery horizontal accordion

I'm attempting to create a simple horizontal accordion-style element. My setup includes three 'banner' divs and three 'area' divs. Ideally, when I click on a banner, the corresponding area should animate - expanding in width to au ...

Unable to convert JSON data into a datatable

My code looks like this: $.ajax({ type : 'POST', url : 'classfication_of_productjson.html', dataType : 'json', data : { "search_bunrui_code ...

Is there a way to add Internet Explorer specific stylesheets to the Wordpress functions directory using enqueue?

I'm facing challenges setting up my Wordpress theme to be compatible with early versions of Internet Explorer. Most online tutorials have advised me to enqueue specific I.E stylesheets in the header, but this method has not been successful for me. Add ...

Using AJAX to send both data input values and file attachments simultaneously to a PHP server

I am currently having an issue with sending form values to my PHP page using AJAX. My form includes text inputs and a file input for image uploads. Below are the AJAX codes I have been working on: function sendval() { var form = $('#user_update_for ...

Utilizing jQuery and Bootstrap CDNs as the source in the index.ejs file

As a beginner in web development, I was tasked with creating a website using node-js & express during my semester. My team and I decided to use a pre-designed website template that included files like bootstrap.min.js, bootstrap.min.css & jquery-2. ...

Submit a Bootstrap modal with AJAX and receive blank $_POST data

After setting up my modal form with pre-defined data, I encountered an issue when trying to submit it with ajax. The form.php script was receiving an empty $_POST array even though the submit button was being clicked. What could be causing this problem? & ...

What could be causing my custom WordPress route to not retrieve data from the database properly?

Within the following code snippet lies a custom endpoint that aims to extract data from the saic3_LibraryIndex database. <?php add_action('rest_api_init', function () { register_rest_route('libraries/v1', '/all', arra ...

html search table td

How can I perform a search in a specific column of a table? Here is the JavaScript script and input tag that I am using: <script type="text/javascript"> function searchColumn() { var searchText = document.getElementById('searchTerm ...

Retrieving table information using curl and regular expressions

Here is the code I have developed to extract data from a table on a specific website. However, I am looking to remove any links present in the data and also separate the title and price into an array. <?php $ch = curl_init("http://www.digionline.ir/ ...

Printing the HTML Template of a widget with multiple loops results in a blank first page being displayed

I have encountered an issue while working with a table and ng-repeat loops in my report widget. The table displays fine on the screen, but when I try to print it, there is always a blank page at the beginning. Interestingly, if I remove the second and thir ...

What is the best way to implement a keypress event in this code?

As a JavaScript and jQuery newbie, I have a question regarding adding a simple key press action to this code. How can I implement functionality where pressing the Right Arrow key takes me to the next image, and pressing the Left Arrow key takes me to the p ...

Ways to display HTML code on a webpage

As part of my internship, I am tasked with creating a WYSIWYG editor that can incorporate logic such as if-statements and loops. While there are existing WYSIWYG editors available, none of them seem to support this type of functionality. I am seeking tech ...

Output based on conditional regex in Jquery

Can I use regex and jQuery to dynamically replace numbers within square brackets with images based on the number? For example: [1] would be replaced with <span><img src="1.png"></span> If we have [11], it should turn into <span>& ...

What is the best way to flip the direction of the text input for a Calculator?

I am currently working on creating a basic calculator from scratch using HTML, CSS, and JavaScript. I have been trying to figure out how to align the numbers to the right side of the input element. Can anyone provide me with guidance on how to achieve thi ...

If TextBoxes overlap, the one that is defined earlier in the ASPX file cannot be clicked on

Managing the visibility of multiple controls can be tricky, especially when they have overlapping coordinates. Take for example two TextBoxes, txtName and txtEmail, positioned at the same x/y coordinates: <div style="position: absolute; top: 55px; ...

What a great method to execute a button click within the same button click using jQuery!

Here's an example of code that attempts to make an ajax call when a user clicks a button. If the ajax call fails, the button should be reclicked. I've tried this code below, but it doesn't seem to work. $("#click_me").click(function(){ ...

The scroll bar of the Bootstrap modal keeps playing peekaboo every time a new modal is triggered

After trying numerous solutions without success, I am posting this question regarding an issue with the scrolling behavior of Bootstrap modals. Here is the scenario: I have 2 Modals: Modal-1 and Modal-2. When a user is inputting information in Modal-1 ...

Updating radio button color upon selection in Boostrap

Looking to customize the colors of radio buttons found on this Bootstrap page By default, the background color is white and changes to blue when clicked. <div class="btn-group" role="group" aria-label="Basic radio toggle button ...

Changing the cursor while the onDrag event is active in ReactJs

I want to implement Drag functionality on certain elements in my application, but I am facing an issue where the cursor changes during dragging. How can I change the cursor specifically when onDrag is active? ...

problem encountered when attempting to fetch a value through ajax

Here is an updated version of the code for my preview problem. The goal is to send and retrieve a value using ajax, but I'm encountering issues with sending the value and getting ajax to work properly. This revised code is easier to test on different ...