Accordion menu causing Javascript linking problem

After following a tutorial, I managed to create an accordion menu in JavaScript.

In the current setup, each main li with the class "ToggleSubmenu" acts as a category that can hide/show the sub-lis.

Now, my query is this: How can I retain the link functionality for ToggleSubmenu while still allowing it to toggle the visibility of other subcategories?

The challenge lies in keeping the functionality intact while not removing the 'return false' statement that currently blocks the link behavior.

Additionally, I'm looking to add a class named ".linked" to specify when a specific link should be activated. If this class is absent, the default behavior should remain.

Your assistance on this matter would be greatly appreciated.

- Benj

$(document).ready( function () {
// Hiding all submenus except for the one designated to open at load:
$(".navigation ul.subMenu:not('.open_at_load')").hide();

// Replacing span elements within list items with links:
$(".navigation li.toggleSubMenu span").each( function () {
    // Storing text content of span element:
    var TexteSpan = $(this).text();
    $(this).replaceWith('<a href="" title="Show submenu">' + TexteSpan + '<\/a>');
});

// Modifying the click event on linked list items with the class "toggleSubMenu":
$(".navigation li.toggleSubMenu > a").click( function () {
    // If the submenu was already open, we hide it:
    if ($(this).next("ul.subMenu:visible").length != 0) {
        $(this).next("ul.subMenu").slideUp("normal", function () { $(this).parent().removeClass("open") });
    }
    // If the submenu is hidden, we close others and show it:
    else {
        $(".navigation ul.subMenu").slideUp("normal", function () { $(this).parent().removeClass("open") });
        $(this).next("ul.subMenu").slideDown("normal", function () { $(this).parent().addClass("open") });
    }
    // Preventing the link from executing:
    return false;
});

Answer №1

My recommendation would be to utilize data attributes. Incorporate a data attribute for each accordion button you wish to link (e.g. name it data-linkedaccordion).

Set up a unique click function on each element with this specific data attribute to launch a new window, with the URL specified in the data attribute value.

This approach will seamlessly integrate with the current accordion functionality without causing any disruptions.

Answer №2

One way to achieve this is by adding the following line of code in your function:

return $(this).hasClass("linked");

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

Unable to access parameters from Pug template when using onclick event

I am facing an issue with my pug template test.pug. It contains a button that, when clicked, should call a function using parameters passed to the template from the rendering endpoint. Below is the structure of my template: doctype html html head tit ...

Guide for displaying a React Bootstrap modal within a function

Currently, I am integrating a React Bootstrap modal popup into my project. The goal is to have the modal display when a user submits a form. The specific modal component I am using is called SaveConfirmationModal. import { Button, Modal} from "react- ...

Successfully running npm update on a Mac seemed to have updated the package, however, the

I needed to upgrade my npm version using the following command: npm install npm@latest -g After running the above command, I encountered the following output: /Users/ariful.haque/.npm-global/bin/npm -> /Users/ariful.haque/.npm-global/lib/node_modules ...

Conceal the second click action within the anchor tag

Is there a way to hide the second click event on all anchor tags except those that trigger popupfun? I have a sample page set up. [Check out the JS Fiddle here][1] http://jsfiddle.net/ananth3087/LgLnpvf4/15/ Link Anchor Tags: The page includes two ...

Simultaneously sending jQuery.ajax data while submitting a form

I'm facing a bit of a dilemma here. I have a form with multiple fields, including one for entering links. When you input a link and click the add button, the link is added to a link_array using jQuery. My goal is to send this array through the jQuery. ...

What steps can I take to streamline and simplify this tab controller code?

I'm looking to simplify this jQuery code because I feel like there's repetition. As someone new to JavaScript and jQuery, I've created two tabs with their respective containers containing miscellaneous information. My goal is to have each co ...

Error 404 in Cordova AJAX请求

As I work on developing an android application with cordova, AJAX requests are essential for the functionality. Utilizing jQuery for these requests, I updated the security policies in index.php to allow connection to a remote server. However, upon initiati ...

I'm looking to center my btn button vertically and horizontally over two images using Bootstrap 5 in a responsive manner. How can I achieve this?

Any tips on how to position my btn button both vertically and horizontally in the center on top of two images? I want it to look like this: https://i.sstatic.net/Rezrd.png This is the code for my button: <a class="btn btn-primary" href=" ...

Retrieving data attributes from model within a Backbone view using Underscore template

I have a Backbone.js view that I'm working with: var StreamV = Backbone.View.extend({ tagName: 'div', className: 'stream', events: { }, initialize: function () { this.listenTo(this.model, 'change' ...

Why are the radio buttons not aligned with the text in the center?

Currently, I am in the process of building a form that includes radio buttons. However, I've encountered an issue where the text next to the radio buttons appears on a different level than the buttons themselves. How can I go about fixing this partic ...

Bootstrap 3 and Angular UI.Bootstrap Accordion working together

Although I am aware that ui.bootstrap is not fully adapted to Bootstrap 3 yet, my app is mostly built using it and reverting back to version 2.3 just for this component is not an option. Therefore, I am currently exploring my options. Here's what I ...

The issue of JW Player Image not appearing in the jQuery Cycle plugin is limited to Internet Explorer only

There seems to be an issue with displaying the preview image assigned to JW Player in IE (both 7 and 8) when embedded in a slideshow using JW Player & jQuery Cycle. Other browsers show the preview image without any problem, and videos not in a cycle di ...

The delete button is designed to remove the record from the database while keeping it visible on the frontend

When the delete button is pressed, it successfully removes the record from the database. However, it does not disappear from the frontend until the page is reloaded or refreshed. This is what I see: @foreach (var item in Model) { <a href="#" ...

"Utilize PHP and jQuery to seamlessly add new records and images to your

Below is the jquery code I am using: $.post('action/edit_breakdown.php', {id: id,program: program,st_name: st_name,p_name: p_name,fob_name: fob_name,track_no: track_no,date: date,currency: currency,image_name: image_name}, function(data){ ...

"Explore the versatility of React Day Picker with customizable months and weekdays_long

I have implemented the following packages: "react": "^18.2.0", "react-day-picker": "^8.1.0", and I am attempting to translate the months and days into French. However, despite passing the translated arrays to my < ...

Retrieve all the records from the collection that have a specific reference number in their ID field

Is it feasible to pull together all documents with an ID that includes a specific value? I am working with Angular 7. I attempted using db.collection('items').where.. but unfortunately, this method is not supported. For instance: (collection) ...

Passing a DOM element from Selenium to JQuery and retrieving the results in C#

I recently encountered some challenges with using JQuery to search for information and send it back to Selenium C# in my project. After some trial and error, I was able to figure it out and wanted to share my findings. Specifically, I focused on: How ca ...

Position the Bootstrap Button on the right side of the navbar

Hey there, I'm completely new to the world of HTML/CSS and this is my first project ever. I welcome any constructive criticism that can help me improve in the future. Now onto my current dilemma: I've decided to use Bootstrap because it seems u ...

Leverage JavaScript to showcase Django object on the webpage

I am trying to achieve the following using javascript: when a row is clicked, it should retrieve the index and display the object at that index. This functionality works in a django template. <div>{{ project.0.customer_name}}</div> <div> ...

Using an Angular route to trigger the resolution of data from a service

I'm having trouble figuring out how to implement Angular route resolve. The documentation does not provide much help for more complex aspects of the javascript framework like this. Here is the service I am using: app.service("AuthService", ["$http", ...