Apply the active class to a section in the menu

I am exploring how to dynamically add the "active" class to a specific section of my menu based on which link within that section has been clicked. After successfully implementing the active class functionality for individual links using jQuery, I am confident that I can adapt the code to achieve my desired outcome. Below is the code snippet that serves as my reference point.

For instance, when the "lil shorty" link is clicked, I aim for the ul with id="section_3" to receive the active class. Similarly, if "Deriving functions" is selected, I intend for the ul with id="section_2" to be marked as active (while removing the active class from section_3). It is important to note that when navigating to the Deriving functions page, section_2 should retain the "active" class.

Your assistance in this matter would be highly appreciated.

Sincerely, Jack

$(function() {
  var pgurl = window.location.href.substr(window.location.href
                    .lastIndexOf("/") + 1);
  $(".section li a").each(function() {
    var aurl = $(this).attr("href").substr($(this).attr("href")
                      .lastIndexOf("/")+1);
    if (aurl == pgurl || aurl == '')
      $(this).addClass("active");
  })
});
<div class="menu">
<ul class='section' id='section_2'>
<li><span id='section_title_2' class='section_title'><a href='#' id='section_link_2'>Against the odds.</a></span>
<ul>
<li id='exhibit_1' class='exhibit_title'><a href="../against-the-odds/introduction"> &rarr; Introduction</a></li>
<li id='exhibit_2' class='exhibit_title'><a href='../against-the-odds/deriving-functions'> &rarr; Deriving functions</a></li>
<li id='exhibit_3' class='exhibit_title'><a href='../against-the-odds/exploiting-odds'> &rarr; Exploiting odds</a></li>
</ul>
</li>
</ul>
  
<ul class='section' id='section_3'>
<li><span id='section_title_3' class='section_title'><a href='#' id='section_link_3'>Remembering everything.</a></span>
<ul>
<li id='exhibit_104' class='exhibit_title'><a href='#'>black swans</a></li>
<li id='exhibit_104' class='exhibit_title'><a href='#'>lil shorty</a></li>
</ul>
</div> 

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

Displaying different content inside a div based on the selection made in a dropdown menu

I'm experiencing difficulties with a jQuery script that should display the content of a div based on selected options in a drop-down menu. The drop-down menu is supposed to provide numerical values for a calculator to determine a loan amount. However ...

PHP script not recognizing CSS styles

My coding dilemma involves a script that is supposed to display numbers from 1 to 100 in black font. For multiples of 3, it should show "Three" in green, and for multiples of 7, it should display "Seven" in blue instead of the number. If a number is a mult ...

Tips on postponing the loading of an iframe within a collapsed div using jQuery until the div/button is clicked

So I'm dealing with a bunch of these collapsible divs on a single page, each containing an iframe. (I used some CSS to make it look like a button when clicked). The issue is that the page is loading very slowly because it's loading all the domai ...

The Angular Node server is responding with the error message "You have entered 'undefined' instead of a stream."

Using angular 9 + universal has been smooth sailing until I encountered an issue after building the app with npm run build:ssr and attempting to run it using node: node dist/app/server/main.js. The error message that popped up in the terminal was: Node ...

What causes my variable to show None instead of the current stock price?

I attempted to extract the stock price data from using BeautifulSoup. However, my program is not displaying the stock price and instead showing "None". How can I correctly retrieve the stock price information? My provided code snippet: from bs4 import Bea ...

Error 404: Troubleshooting an Azure Issue with Custom TrueType Font in .NET MVC 5

I am encountering an issue that has been previously addressed. Despite implementing the suggested solutions, the problem persists. In an effort to enhance my project, I downloaded two custom ttf fonts called Monterey and Bakery. Both fonts have been succ ...

I'm having trouble with my JavaScript function when it is called simultaneously by two different sources

In this scenario, two calls to the function are made simultaneously. First call: triggered by setInterval, skips the animation, Second call: executed after $(document).ready and performs correctly. fadeInFadeOut : function (oldContent, newContent, flag) ...

The 'classProperties' React component module is currently not enabled

Encountering an error while running my react module 'classProperties' isn't currently enabled (44:11): } 43 | // componentDidUpdate or try this > 44 | onClick = (e) => { | ^ 45 | e.preventDefault(); 4 ...

"Learn how event bubbling in jQuery works with the use of .delegate() or .live() methods

After numerous attempts to simplify a code execution, I find myself struggling with loading DOM objects from the server using the .load() function. Specifically, I am encountering issues with implementing a datepicker plugin called jdpicker on an input tex ...

Using gridstack-angular.js with the Hot Towel Template: A Comprehensive Guide

I recently started using the Hot Towel Template and decided to incorporate the gridstack-angular package. I am attempting to implement the demo functionality into my-page.controller.js, my-page.route.js, and my-page.html. After loading the dependencies wi ...

Choosing a sequence of text snippets divided by the <br> tag found inside a specific class

Is there a way for me to extract each of those strings using jQuery or JavaScript? For example, 'fight club', 33 Main Street, Houston, etc. <span class="cart_text2"> Fight Club <br> 33 Main Street, <br> Hou ...

In search of the mean value using PHP, jQuery, and AJAX technologies

I have successfully created a star rating system using PHP and jQuery. The issue arises when attempting to display the average rate for a specific item that I am rating; instead, the average value printed is for all items being rated. Here is my jQuery co ...

In Firefox, resizable child elements in a flex container cannot be resized

Exploring the realm of flexbox has brought me to a bit of a roadblock with a layout like this one: https://jsfiddle.net/5b0pLgkj/ Everything seems to be running smoothly in Chrome and Safari, where children elements can be resized and fill up the availabl ...

Denied rendering of design due to incompatible MIME type

Just delved into the world of node / express and decided to test my skills by creating a simple tip calculator app. However, I seem to have hit a roadblock with loading my CSS. The console keeps throwing this error: "Refused to apply style from &apos ...

Deactivate the second subradio button when the first option is selected and vice versa

Need some assistance, hoping for your help. I have 2 choices with subcategories. 1 - Option A ---- variant 1 ---- variant 2 ---- variant 3 2 - Option B ---- variant 4 ---- variant 5 ---- variant 6 Check out my code here Users must choose betwe ...

Transform a single unordered list with list items using Jquery into a nested list structure by utilizing a specific class

I need to transform an unordered list into a hierarchical structure based on pre-assigned classes. The original list looks like this: <ul> <li class="level-1"><a href="#">First Level Item</a></li> ... <li class="leve ...

What could be causing this minimal Angular - WebTorrent configuration to fail?

The setup appears to be quite straightforward. Check out the Webtorrent usage reference here This is how I have my setup: import WebTorrent from 'webtorrent'; @Component({ selector: 'app-root', standalone: true, template: `bla` ...

Using jQuery to display or hide a facebox with an IF statement

Uncertain if I completed this task correctly, but here is my attempt on JSfiddle: http://jsfiddle.net/EQ4mA/2/ Let me start off by saying that I have minimal experience with jQuery (I am skilled in html/css), but due to our team losing development hours, ...

The code functions properly when run locally, however, it encounters issues when deployed

When testing the following lambda code locally using Alex-app-server, everything works perfectly fine. However, when I publish it and test on AWS Lambda, it gets stuck within the else statement. It prints the console log 'OUT PUBLISH' but doesn&a ...

How can you retrieve a value from a JavaScript closure function?

I'm struggling with getting a value from a closure function in Javascript. In my initial attempt, I placed the return statement inside the inner function, but it was not effective. Then, I tried moving the return statement to the outer function, howev ...