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

jQuery returns varying values for checked status when using click() method versus manual click

I'm facing an issue with a checkbox generating dynamic content. Whenever I try to pre-create the dynamic content on page load by using click(), the "checked" attribute is not set until after the click function finishes. Strangely, when I manually cli ...

Is it possible to modify the value of a non-HTML attribute in an input field using jQuery?

I have a text input field that looks like this: <input type="text" value="" somethingSpecial="filename.txt" id="myID" class="box"> There is also a select list as shown below: <select name="type" class="form-select" id="type"> <option ...

Tips for submitting multiple radio button values in a tabular format

HTML Code: <div class="tab"><h4>Zip Code</h4> <p><input type="text" class="text1" placeholder="Enter zip code..." oninput="this.className = ''" name="zipcode"></p> </div> <div class="tab">& ...

Having trouble finding rows to manipulate in an HTML table generated using jQuery csvToTable?

As a beginner in programming, I find myself seeking assistance with a particular issue. I have successfully read a CSV file stored locally and transformed it into an HTML table using jQuery csvToTable http://code.google.com/p/jquerycsvtotable/. Additional ...

The scrollbar will be visible only when the mouse hovers over the table

I have been experimenting with customizing the scrollbar appearance of an ant design table. Currently, the scrollbar always displays as shown in this demo: https://i.stack.imgur.com/vlEPB.png However, I am trying to achieve a scroll behavior where the sc ...

Typescript Angular2 filtering tutorial

In Angular 2 using TypeScript, the goal is to search for matching values from an array within an object array. The intention is to filter out any objects where the 'extraService' property contains any of the values from the 'array_values&apo ...

Implementing Ajax to Load Template-Part in Wordpress

Hey there! I'm currently working on enhancing my online store by adding a new feature. What I'd like to achieve is that when a customer clicks on a product, instead of being taken to the product page, the product details load using AJAX right on ...

Using jQuery to make an asynchronous request for a large file can cause the browser to become

After initiating a jQuery $.get request to retrieve data from an HTML file, I have utilized the success function to filter out a select element's options and present them as paragraphs within div elements that are subsequently appended to my markup. A ...

Ensuring Input Integrity: Utilizing HTML and Javascript to Block Unfilled Form Submissions

Currently, I am working on developing a registration page using HTML and JavaScript. Although I have added the 'required' tag in HTML to prevent users from submitting empty input fields, I noticed that users can bypass this restriction by simply ...

Utilizing HTML templates in Express instead of Jade

Currently in the process of setting up a new MEAN stack project, with Angular as my chosen front-end framework. I am aiming to utilize HTML files for my views in order to incorporate Angular within them. However, I am facing challenges when attempting to s ...

Please enter only numerical values using jQuery

Currently, I am facing a slight issue. My goal is to only run the code when the input characters are numbers. This is the snippet of code I have been working with: $(".jq-sales, .jq-variablecosts, .jq-fixedcosts, .jq-additional-sales, .jq-sales-units, .j ...

The Angular service encounters issues when interacting with REST API

Within my application, a template is utilized: <div class="skills-filter-input" ng-class="{'hidden-xs': skillsFilterHidden}"> <input type="text" ng-model="skillQuery" ng-change="filterSkills()" placeholder="Filter skills" class="filter- ...

JavaScript code for transitioning between a thumbnail and a full-width image header on a webpage

I am looking to create transitions in NextJs that involve a smooth shift from a thumbnail image to a full-screen header when clicking on a project from the home page. I prefer utilizing internal routing rather than React Router, but I am open to using that ...

Want to develop a web application and incorporate Ajax into it?

I'm sure many of you have created an online application that streamlines processes and saves time and money for your company. So, how did it go when you added some Ajax to enhance the user experience after developing the application? Any recommendat ...

document ready function in the Microsoft AJAX client side library

The Microsoft AJAX client-side library contains various functions that imitate the server-side Page Life Cycle and conditions like if (!Page.IsPostBack). I am unsure about which client-side function corresponds to the equivalent server-side method. What is ...

Choose a Range of DOM Elements

My challenge is to select a range of DOM elements, starting from element until element. This can be done in jQuery like this: (Source) $('#id').nextUntil('#id2').andSelf().add('#id2') I want to achieve the same using JavaScr ...

Connecting two divs with lines in Angular can be achieved by using SVG elements such as

* Tournament Brackets Tree Web Page In the process of developing a responsive tournament brackets tree web page. * Connection Challenge I am facing an issue where I need to connect each bracket, represented by individual divs, with decorative lines linki ...

Loading/Adding JS script in an asynchronous manner

In my Laravel project, I am implementing templates for different pages based on a main page. Each page requires different JS scripts to function properly, so I created a template to import these scripts: <!-- jQuery 2.1.3 --> <script src="{{ URL: ...

Issue with Jquery modal not functioning properly on second attempt

Currently, I am working on developing an application using CodeIgniter. However, I have encountered a problem where the modal window does not open for the second time. Here is a more detailed explanation of the issue: The form (view) in question contains ...

It seems that Firefox is ignoring the word-wrap style when the class of a child element is changed

Take a look at this: var iconIndex = 0; var icons = ['check', 'chain-broken', 'flag-o', 'ban', 'bell-o']; $('button:eq(0)').click(function() { iconIndex = (iconIndex + 1) % icons ...