Enable the option to customize the icon of the recently activated sidebar menu item

I need help with changing the arrow icon of a menu-item only when it is clicked, without affecting all other menu-items.

In the image below, you can see that currently, clicking on any menu-item changes all the arrows.

Attached Image

//sidebarMenu
jQuery('#sidebar .sub-menu > a').click(function() {
  var last = jQuery('.sub-menu.open', jQuery('#sidebar'));
  jQuery('.menu-arrow').removeClass('arrow_carrot-right');
  jQuery('.sub', last).slideUp(200);
  var sub = jQuery(this).next();
  if (sub.is(":visible")) {
    jQuery('.menu-arrow').addClass('arrow_carrot-right');
    sub.slideUp(200);
  } else {
    jQuery('.menu-arrow').addClass('arrow_carrot-down');
    sub.slideDown(200);
  }
  var o = (jQuery(this).offset());
  diff = 200 - o.top;
  if (diff > 0)
    jQuery("#sidebar").scrollTo("-=" + Math.abs(diff), 500);
  else
    jQuery("#sidebar").scrollTo("+=" + Math.abs(diff), 500);
});
.arrow_carrot-up,
.arrow_carrot-down,
.arrow_carrot-left,
.arrow_carrot-right {
  font-family: 'ElegantIcons';
  text-transform: none;
  line-height: 1;
  -webkit-font-smoothing: antialiased;
}

.menu-arrow {
  float: right;
  margin-right: 8px;
}
<ul class="sidebar-menu">
  <li class="active">
    <a class="" href="index.html">
      <i class="icon_house_alt"></i>
      <span>Menu</span>
    </a>
  </li>
  <li class="sub-menu">
    <a href="javascript:;" class="">
      <i class="icon_document_alt"></i>
      <span>MenuItem1</span>
      <span class="menu-arrow arrow_carrot-right"></span>
    </a>
    <ul class="sub">
      <li><a class="" href="page1.html">Sub-Item</a></li>
      <li><a class="" href="page2.html">Sub-Item</a></li>
      <li><a class="" href="page3.html">Sub-Item</a></li>
    </ul>
  </li>
  <li class="sub-menu">
    <a href="javascript:;" class="">
      <i class="icon_desktop"></i>
      <span>MenuItem2</span>
      <span class="menu-arrow arrow_carrot-right"></span>
    </a>
    <ul class="sub">
      <li><a class="" href="page4.html">Sub-Item</a></li>
      <li><a class="" href="page5.html">Sub-Item</a></li>
      <li><a class="" href="page6.html">Sub-Item</a></li>
    </ul>
  </li>
  <li>
</ul>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

Any assistance would be greatly appreciated!

Answer №1

When using jQuery, the $(this) function will target only the specific element that was clicked on. For example, if you click on an a tag, the click event will travel up to the nearest parent with the class .sub-menu, allowing you to handle the click event there (although you could also handle it directly on the a tag if needed).

Give this a try:

$('#navigation .sub-menu').click(function () {
    $(this).find('.arrow-icon').removeClass('icon-right-arrow').addClass('icon-down-arrow');
});

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

"Utilize a custom filter in AngularJS to narrow down data based on specified numerical

Can anyone assist me in creating a filter for AngularJS data? I have two inputs, minAgeInput and maxAgeInput. I want to retrieve all products/objects (using ng-repeat) where the product's minimum age and maximum age fall within the limits set by the ...

Ensure the slider functions on all types of browsers

I found a code snippet for an image slider on codepen and integrated it into my project. However, I encountered some issues trying to center the slider in the browser window with 5% space on each side. I attempted using $(window).width(); to set the width ...

Performing function in Vue.js when a change occurs

I recently started developing a Vue.js component that includes an input field for users to request a specific credit amount. My current goal is to create a function that will log the input amount to the console in real-time as it's being typed. Ultima ...

The location.reload function keeps reloading repeatedly. It should only reload once when clicked

Is there a way to reload a specific div container without using ajax when the client requests it? I attempted to refresh the page with the following code: $('li.status-item a').click(function() { window.location.href=window.location.href; ...

Need for input

I am working on organizing my routes in a separate file from app.js. The login route requires access to a Firebase instance. routes/auth.js var express = require('express'); var router = express.Router(); module.exports = function(firebase) { ...

Mesh is making an excessive number of draw calls

After importing some mesh from Blender using the three.js exporter from the utils folder, I noticed that it only has 3 materials but is utilizing 28 draw calls. Why is this happening? I expected it to use only 3 draw calls. View mesh image ...

Positioning JQuery tooltips

I've been developing a simple tooltip tool (check out the fiddle link below), but I'm encountering some issues with positioning. My goal is to have the tooltip appear centered and above the clicked link, however right now it appears at the top le ...

Is there a way to stop the page from scrolling once I reach the bottom of a specific div within it?

My webpage has multiple divs that share a similar structure: <div style="height:200px; overflow:auto;"> <div style="height:300px;"> <!--There is a lot of content here--> </div> </div> When the user hovers ove ...

What are some alternatives to using iframes?

Currently, I am working on a project for a client where I need to display multiple websites on a single page in a browser. To achieve this, I initially used the iframe element, but encountered an issue with the openerp frame. Despite setting up the openerp ...

Using jQuery, stop any click or touchstart events on child elements of the target element

Below is my current jQuery code: $(document).on('click touchstart', function (e) { if ( (!$(e.target).hasClass('sb')) ) { $('#features .sb .screen').animate({ top: '6.438em' }, 150); } } I want to twea ...

Leveraging ES6 Symbols in Typescript applications

Attempting to execute the following simple line of code: let INJECTION_KEY = Symbol.for('injection') However, I consistently encounter the error: Cannot find name 'Symbol'. Since I am new to TypeScript, I am unsure if there is somet ...

Encountering this issue despite confirming the presence of data on the line before! What could be the missing piece here? Error: Unable to access property 'includes' of undefined

Here is the situation.. I'm retrieving data from a database and storing it in an array of objects. These objects represent articles. I am working on implementing a filter system based on categories. The objective is to apply a filter that checks for a ...

Issue with customized jQuery UI image organizer

I have customized the jquery ui photo manager example from their website. I am facing an issue where I want the photo to be draggable and also link to an external page. How can I achieve this? I tried wrapping the image with an tag, but it didn't wor ...

Is it possible for the original object to be altered when passing it as a parameter to a function in a different file?

When you update an object passed as a parameter, will the updates be reflected "upwards" if the method receiving the parameter is in a different file? Or will the object retain its own context despite being passed down? ...

Tips for Smoothing Out Your jQuery LavaLamp Effect

I'm currently developing a jQuery function to implement the "lavalamp" effect on my navigation bar. Everything seems to be working smoothly, except for one issue. If you hover over an item (e.g., Community Service) and then move your mouse away from ...

Does creating a form render the "action" attribute insignificant in an AJAX environment?

When submitting forms exclusively through AJAX, is there any advantage to setting the action attribute at all? I have yet to come across any AJAX-form guides suggesting that it can be left out, but I fail to see the purpose of including it, so I wanted t ...

Issue with Vue template not displaying within a loop

After completing a basic Vue tutorial on setting up a Todo app, I decided to integrate some aspects of it into a website I am developing. However, I have encountered an issue with my for-loop that is not functioning as expected. The project was initially ...

In JavaScript, a variable's value can be assigned to another variable using the variable

I have a collection of predetermined variables. I dynamically assign a value to another variable, which ends up representing the name of one of the predefined variables. This allows me to easily determine which set variable to use on a particular section o ...

Using AJAX, create an item in Rails that can be linked to various categories directly from the categories edit page

` section: Considering items as keys that accept only a name for input. The main reason behind including keys on the categories edit display through ajax is to enable users to modify the category items template. The template contains keys that can be uti ...

Having trouble retrieving data passed between functions

One of my Vue components looks like this: import '../forms/form.js' import '../forms/errors.js' export default{ data(){ return{ form: new NewForm({ email: '&apos ...