How to keep jQuery horizontal submenu open when clicking on the menu

Seeking assistance with a menu issue. I have a horizontal menu with subitems that display when clicked, but having trouble with the submenu behavior. When clicking on a submenu item, I want it to remain open and show the related items underneath it. For example, clicking on "page1" should display the submenu items "page1a" and "page1b", and then clicking on "page1a" should keep the submenu open displaying the rest of the page1 items. Here is the HTML menu code:

<ul id="topnav">
    <li>
        <a href="index.html">Home</a>
    </li>
    <li>
        <a href="#">About Us</a>
        <!--Submenu starts here-->
        <span>
            <a href="#">History</a> |
            <a href="#">Our Team</a> |
            <a href="#">Process</a>
        </span>
    </li>
    <li>
        <a href="#">Services</a>
        <!--Submenu starts here-->
        <span>
            <a href="service1.html">Service 1</a> |
            <a href="#">Service 2</a> |
            <a href="#">Service 3</a> |
            <a href="#">Service 4</a> |
            <a href="#">Service 5</a> |
            <a href="#">Service 6</a>
        </span>
    </li>
    <li>
        <a href="#">Products</a>
        <!--Submenu starts here-->
        <span>
            <a href="#">Product A</a> |
            <a href="#">Product B</a> |
            <a href="#">Product C</a> |
            <a href="#">Product D</a> |
            <a href="#">Product E</a> |
            <a href="#">Product F</a>
        </span>             
    </li>
</ul>

The jQuery function for opening the submenu:

<script type="text/javascript">
var ddmenuitem = 0;
function jsddm_open()
{  
jsddm_close();
ddmenuitem = $(this).find('span').css('display', 'block');
}
function jsddm_close()
{  
if(ddmenuitem) ddmenuitem.css('display', 'none');
}
$(document).ready(function()
{  
$('#topnav > li').bind('click', jsddm_open)
$('#topnav > li > a').click(function(){
if ($(this).attr('class') != 'active'){
$('#topnav li a').removeClass('active');
$(this).addClass('active');
}
});
$('.project-tekst').trimTxt();
});
</script>

CSS styling for the menu:

ul#topnav 
{
float: left;
width: 900px;
background: #00537F;
padding: 0;
margin: 0;
margin-top: 3px;
position: relative;
list-style: none;
font-size: 12px;
}
ul#topnav li 
{
float: left;
margin: 0;
padding: 0;
}
...
(Additional CSS styles omitted for brevity)

Any suggestions or solutions would be greatly appreciated.

Thank you!

Answer №1

I'm a bit confused about the issue at hand.

Here is how I approached it on jsfiddle: http://jsfiddle.net/QQQdH/765/

Initially, I included the current class in the Disciplines menu (as if I were on the Discipline page).

 <a href="http://google.com/" class="current">Disciplines</a>

Next, within the click listener function, I prevented the default action (following the href) when the anchor tag has the 'current' class.

   $('#topnav > ul > li > a').click(function(ev){
    if ($(this).hasClass('current')) {
        ev.preventDefault();
    }

This setup allows me to view the submenu even with a href="something" present.

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

What is the interaction between Handlebars.js, jQuery, and Nodejs Express?

Currently, I am in the process of building a simple website using Express JS and Handlebars. The two main files involved are page1Router.js and page1Router.handlebars. Within the Handlebars file, you will find the following code snippet: <!DOCTYPE html ...

personalizing material-ui component styles – set select component color to be pure white

I am looking to implement a dropdown menu using material-ui components (refer to https://material-ui.com/components/selects/). To do so, I have extracted the specific component from the example: Component return <div> <FormControl variant="outli ...

The CSS code isn't functioning properly on both desktop and mobile views

Here is what I desire to achieve: The CSS code I have is: And the HTML code looks like this: <body> <div class="contanier"> <div id="rightcol">Right Section</div> <div id="content">Content Section</div> < ...

angular material drag and drop triggers a callback once the css transition has completed

I have successfully implemented a list of elements that can be dragged and dropped using Angular Material's drag and drop feature, similar to the tutorial available at this link. In my implementation, I have a "drop(event)" function that not only mov ...

How can you use jQuery to target a textbox based on its value that includes quotation marks?

Dealing with a JavaScript string that includes a quote mark can cause some difficulties. For example, strings like Don't click this checkbox or He said "hi" pose unique challenges when trying to find an exact match in a checkbox value. Consider the H ...

Finding a particular sequence of characters and extracting information that comes after it

A while back, I created a website and now I'm looking to transfer the data into a database without manually copying and pasting the 400+ pages it has accumulated. This way, I can transform the site into a database-driven platform. Each page on my sit ...

The Jquery Addclass function is not functioning properly

I have been struggling with this code as it seems to not be working in any browser, including IE, Chrome, and Firefox. I suspect that the system should add a class, so I checked the DOM using Chrome's console. I did see the class being added, but ther ...

Could there be an issue with my website's bootstrap where badges are not being applied properly?

Currently, I am in the process of learning ReactJS and running into an issue where my bootstrap is not working within my .jsx file. Despite following a tutorial (MOSH) diligently and extensively researching on stack overflow, I have not been able to find a ...

Is there a bug with Kendo UI? The Kendo grid pager seems to be misaligned with the bottom

Recently, I've been experimenting with Kendo UI and encountered a specific issue: The pager for my Kendo grid is not aligning properly at the bottom of the grid. Although I've attempted to adjust the CSS styling for k-grid-pager, the issue persis ...

Exploring the integration of AJAX and jQuery with Django 1.3

I am completely new to the Django framework, web development, and Python. Currently, I am trying to incorporate AJAX into my project but struggling to find a working sample. I need assistance with integrating AJAX or jQuery within a Django 1.3 project. Cu ...

Is it possible to convert xaml to html in a silverlight newsletter?

Currently, I am working on a project that involves creating a webpage where my admin can send emails directly through the website. To achieve this functionality, I have implemented a code snippet to send emails to all subscribers. foreach (Subscription pe ...

The navigation bar buttons in my IONIC 2 app are unresponsive on both the

In Ionic 2 for Android, I encountered an issue with the title placement. To resolve this, I applied some CSS to center the title and added left and right buttons to the nav bar. However, when I tried to implement onclick functionality for these buttons, no ...

Aligning a number in the center both vertically and horizontally within a div that is inside a table-cell

Is there a way to center a number both vertically and horizontally within a div that needs to be 60px in width and height, shaped like a circle, and placed in the center of a table-cell? I've managed to center the div within the table-cell but can&apo ...

Isotope grid shatters when browser window is resized

My goal is to design a 3-column grid using Twitter Bootstrap and Isotope. However, when I resize the browser to force the layout into a single column mode, Isotope fails and leaves large spaces both vertically and horizontally. Regular Layout Issues in ...

Making the Bootstrap 4 card-footer expand to occupy the remaining height of the column

I'm currently developing a project that incorporates bootstrap cards. There are 3 cards, each for a different column, and I need them to have equal heights. Here is how they currently look: The B and C cards should occupy the remaining height of thei ...

Combining DataTables with Moment.js: Calculate total time by adding durations

I am utilizing DataTables to track the amount of time each person spends fundraising and then showcasing their percentage of the funds raised for a camp. My goal is to add up the durations using moment (some durations may exceed 24 hours), calculate the f ...

Tips for customizing icons when hovering in CSS

I'm trying to make a default icon light up on hover using CSS, but I'm having trouble getting it to work. Any suggestions on how I can achieve this effect? Thank you! HTML <ul class='nav nav-main nav-sidebar'> <li role=&apos ...

utilizing ng-option to present choices in a dropdown menu

I have been implementing a scroll-down menu in my application using options. However, I now want to switch to using ng-option to populate the values from a JavaScript file. I also require assistance with writing AngularJS code for this task. Below is a s ...

Utilize two separate functions within the onchange event of a v-checkbox component in a Vue.js application

I am currently using Vue.js with Vuetify and Laravel. In one of my components, I have a dynamic datatable that fetches data from the database using axios. Within this datatable, there are v-checkboxes. Everything is functioning as expected, but now I want ...

Different ways to eliminate unnecessary items in an array

One task I have is to eliminate all duplicate elements within an array, as shown in the example below: var arr = [ {'seriesIndex':1,pointIndex:0}, {'seriesIndex':1,pointIndex:1}, {'seriesIndex':0,pointIndex:0}, ...