I am looking to create a side menu that will allow for dynamic class changes upon clicking

I'm looking to create a dynamic side menu that changes class on click event.

My goal is to have jQuery add a class to a clicked "li" element that doesn't already have the class "active", while removing the class from other "li" elements. I want to achieve this using Html.ActionLink exclusively.

<div id="sidebar">
<ul class="nav nav-pills nav-stacked">
    <li>
        @Html.ActionLink("Home", "Index", "Home", new { @class = "ach" })
    </li>
    <li>
         @Html.ActionLink("About Us", "About", "Home", new { @class = "ach" })
    </li>
    <li>
        @Html.ActionLink("Contact Us", "Contact", "Home", new { @class = "ach" })
    </li>

</ul>

    Here is the JavaScript code I used:



 $('.ach').on('click', function () {
    debugger;
    $('li').removeClass('active');
    $(this).parent().addClass('active');
});

Answer №1

<div id="menu">
<ul class="nav nav-tabs nav-stacked">
    <li>
        @Html.ActionLink("Main", "Index", "Main", new { @class = "mch",id="mainLink" })
    </li>
    <li>
         @Html.ActionLink("About Me", "About", "Main", new { @class = "mch",id="aboutMeLink" })
    </li>
    <li>
        @Html.ActionLink("Contact Me", "Contact", "Main", new { @class = "mch" ,id="contactMeLink"})
    </li>

</ul>

 $('.mch').on('click', function (event) {
    debugger;
    $('li').removeClass('active');
    var id = event.target.id;
    $('#'+id).addClass('active');   

});

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

Can you explain the concept of a factory function to me?

As I delve into learning Javascript, I have encountered terms like factory functions and constructor functions in the realm of object-oriented programming. Despite my attempts to grasp the distinction between them, I still find myself struggling to fully ...

Create an array by copying elements from another array object

My goal is to merge the data from two arrays, array1 and array2, into a new array called array3. Here's how I want it structured: array 1 objects: userName, userId array 2 objects: userId, msg I aim to obtain an array3 consisting of: userId, userNa ...

Executing the ES6 import syntax within a Node child process

After many attempts, I have come to the conclusion that I am ready to throw in the towel. My goal was to run a node es6 project that employs es6 import syntax; however, it seems that the child processes are not cooperating. The issue stems from the fact th ...

The operation of Ajax can be intermittent, as it may run at

I'm encountering an issue with my ajax code. I am adding data from my database and attempting to refresh a div element. It seems to work sometimes but not consistently. Why is that? Here's the problem - I have a function called addtoqueue. Insid ...

Failed to decipher an ID token from firebase

I'm feeling extremely frustrated and in need of assistance. My goal is to authenticate a user using Google authentication so they can log in or sign up. Everything worked perfectly during development on localhost, but once I hosted my app, it stopped ...

Page Not Found: Troubleshooting Express Server Issue

Currently, I am working on creating a basic parser using Node/Express and Cheerio. However, even though the server is running smoothly, I am unable to load any pages in my browser. Below is the code snippet from server.js: var express = require('expr ...

Data from the table will be utilized in an SQL query according to a particular table row

My PHP script is set up to query my database and pull data based on the value of the acceptance field. SELECT * from database WHERE acceptance = 1 This process is working smoothly. Following that, my PHP script loops through the retrieved object to popul ...

How to display information from a JSON file using dynamic routing in a React.js application

I'm currently working on a project to replicate Netflix using reactjs, but I've hit a roadblock and can't figure out what to do next. I've tried watching YouTube tutorials and reading articles online, but I haven't been able to fin ...

Exploring the wonders of useState in React/JavaScript - a comprehensive guide

I encountered an issue while attempting to map an API request from a useState hook. The fetch operation functions correctly and returns an array of objects that I then assign to my useState variable. Subsequently, when I try to map over the useState varia ...

Create objects in the gallery

I recently developed a React Material-UI component using Typescript: <Grid container direction="row" justifyContent="flex-start" alignItems="flex-start"> <Grid item xs={5}> <B ...

Encountering problem with image file encoding while using res.download in Express.js

My node.js server with expressjs is set up for local development, where I store and retrieve various files through basic HTTP calls. Most of the time, everything works smoothly. However, on rare occasions, a small number of files return to the end-user sig ...

"Looking to utilize the .NET Framework default SMTP service within Azure Functions? Learn how to integrate Postal in your Azure Function for seamless

We are in the process of transitioning certain background methods responsible for sending emails to Azure functions. Currently, our email functionality is handled using Postal Postal relies on .NET Framework’s SmtpClient which traditionally has connecti ...

The functionality of jQuery on dropdown list change seems to be malfunctioning in Codeigniter

As a novice in CodeIgniter, I've scoured various StackOverflow threads for a solution to my issue without any luck. Below is the view code causing trouble: <select name="select1" id="select1"> <option value="0">None</option> ...

Struggling with Three.js raycasting issues

My mesh is positioned at the origin. var textureCanvas = new THREE.CanvasTexture( imageCanvas ); textureCanvas.repeat.set( 4, 4 ); textureCanvas.wrapS = THREE.RepeatWrapping; textureCanvas.wrapT = THREE.RepeatWrapping; var materialCanvas = new THREE.Mesh ...

Is it possible to add additional text to an input field without modifying its existing value?

I have a numerical input field labeled "days" that I want to add the text " days" to without altering the actual numerical value displayed. <input type="number" class="days" (keyup)="valueChanged($event)"/> Users should only be able to edit the num ...

Performing an API request and saving the response data into a

I’m struggling to figure out how to make an API call with request in MongoDB using Mongoose. I’m new to this topic and could really use some help! Here is my test.js file, does anyone have any ideas on how to solve this issue? My goal is to save the AP ...

Efficiently run multiple Node-written apps on a single server

I currently have a single VPS and would like to host multiple node.js apps on it, similar to how Apache or Nginx works. I am using Nginx as a proxy, but I have concerns. As you know, one of the key features of Node.js is its non-blocking I/O and sing ...

Extracting data from an array using Angular

Currently, I am developing an Angular application that involves handling an array structured like the one below. [ { Code: "123", Details:[ { Id: "1", Name: "Gary" }, { ...

Retrieve the element's name with jQuery

In my form, there is a select element with the name field_p_payment[value] and I am trying to reset this select box. I attempted to do this by using the code below: document.getElementsByName("field_p_payment[value]").selectedIndex='0' Unfortun ...

Summon wicket from the non-wicket-component entity

I am currently utilizing js-lib to transform text into a rich-editor, complete with options such as "bold", "italic" and more. The RichEditor also includes an UploadImage button, for which I am able to modify the call-back function: function startUploadi ...