Ways to rotate SVG images exclusively upon clicking

After experimenting with rotating SVG images on my navbar buttons, I encountered an issue. Whenever I click one button, all the images rotate simultaneously. Additionally, clicking elsewhere does not reset the images to their original positions.

This is the HTML code:

<!-- === Dropdown Menu === -->
<div class="collapse navbar-collapse" id="navToggler">
  <div class="dropdown">
    <button class="btn" type="button" id="navButton1" data-bs-toggle="dropdown" aria-expanded="false">
      Product <img class="arrow reset" src="/blogr/images/icon-arrow-light.svg">
    </button>
    <ul class="dropdown-menu" aria-labelledby="navButton1">
      <li><a class="dropdown-item" href="#">Overview</a></li>
      <li><a class="dropdown-item" href="#">Pricing</a></li>
      <li><a class="dropdown-item" href="#">Marketplace</a></li>
      <li><a class="dropdown-item" href="#">Features</a></li>
      <li><a class="dropdown-item" href="#">Integrations</a></li>
    </ul>
  </div>
  <div class="dropdown">
    <button class="btn" type="button" id="navButton2" data-bs-toggle="dropdown" aria-expanded="false">
      Company <img class="arrow reset" src="/blogr/images/icon-arrow-light.svg">
    </button>
    <ul class="dropdown-menu" aria-labelledby="navButton2">
      <li><a class="dropdown-item" href="#">About</a></li>
      <li><a class="dropdown-item" href="#">Team</a></li>
      <li><a class="dropdown-item" href="#">Blog</a></li>
      <li><a class="dropdown-item" href="#">Career</a></li>
    </ul>
  </div>
  <div class="dropdown">
    <button class="btn" type="button" id="navButton3" data-bs-toggle="dropdown" aria-expanded="false">
      Connect <img class="arrow reset" src="/blogr/images/icon-arrow-light.svg">
    </button>
    <ul class="dropdown-menu" aria-labelledby="navButton3">
      <li><a class="dropdown-item" href="#">Contact</a></li>
      <li><a class="dropdown-item" href="#">Newsletter</a></li>
      <li><a class="dropdown-item" href="#">LinkedIn</a></li>
    </ul>
  </div>
</div>
<!-- END -->

CSS:

.rotate {
   transform: rotate(180deg);
   transition: .3s;
}
.arrow.reset{
   transform: rotate(0deg);
   transition: .3s;
}

Javascript:

$("button.btn").on("click", function () {

      $(".arrow").toggleClass("rotate");
      $(".arrow").toggleClass("reset");

});

Desired behavior on click:

Current behavior on click:

Behavior when clicking elsewhere:

Answer №1

When you click on the button, the code is currently selecting all elements with the class .arrow in the entire document. It seems like what you actually want to do is target the arrow within the clicked button only.

To achieve this, you need to provide a second argument to jQuery specifying that you want to search for the element only inside the clicked node. You can modify the code like so:

$('button#navButton1.btn').on('click', function (event) {
    $('.arrow', event.target)
        .toggleClass('rotate')
        .toggleClass('reset');
});

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

Is the absence of http(s) in <link ...> causing any issues?

I recently noticed a peculiar link on the Font Awesome homepage that seems to work without including http or https. <link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet"> This made me wonder, what does // a ...

Unable to interact with a button element of type="button" using Selenium, but successful with elements of type

In my attempt to interact with the webpage, I am specifically focusing on this button: <button class="button-text-like marg-l-0 float-l desktop-only remove-button" data-bind="click: $root.removeCartItem" type="button"> <i class="gicon-cross">& ...

Using Switch Case and If Statements in Javascript and Jquery

Can you help me troubleshoot this code? It's designed to detect clicks and keypress events within the #ts_container div for buttons and anchors. The goal is to use an If statement in each case to determine if it's a click or keypress, then update ...

Is there a way to dynamically enable ui-sref through element.append in Angular?

I am in the process of developing a pagination directive. Once the total_for_pagination data is filled, the appropriate HTML for the pagination gets generated. Here's my current HTML structure with a maximum of 50 per page: <pagination data-numbe ...

Combining Multiple Optional Async AJAX Calls

In my Angular 1.5.8 app, I have different views that require various combinations of the same 3 ajax requests. Some views need data from all three endpoints, while others only need data from one or two. To efficiently manage the retrieval of this data, I ...

Disabling Commands using Discord JS Commando in a guild

Curious about something. Will Discord JS Commando disable a command only within a specific server (guild) or globally across all servers? ...

Utilizing Jquery Tooltipster to Interact with DOM Elements Post-AJAX

I've been encountering some issues with the Tooltipster jQuery plugin and AJAX: Clicking on a link to launch Tooltipster works fine Receiving a form through AJAX is also successful However, I am unable to interact with the input fields in my form Be ...

Utilizing the Kraken.com API to create a message signature with AngularJS

I'm currently tackling Angular2 and for my first project, I want to tap into the Kraken.com API. (I know, I could have chosen an easier task :) The "Public" calls are working smoothly, but I've hit a snag with the "Private" methods. (Those requi ...

Show an AJAX loading animation for a minimum of 2 seconds

I've been struggling to find a solution for this issue and none of the resources I've looked at have provided a satisfactory answer. Currently, I am using .load calls to fetch data from the database and display it in a div with the class "conten ...

Ways to prioritize HTML5/CSS3 navigation menu above the content

I am attempting to position my navigation menu on top of the content. Despite using z-index and overflow = visible, the desired effect is not achieved. I want to avoid setting the position relative as it will push the content downwards. Simply put, I want ...

Troubleshooting problems with data rendering in jQuery

Currently, my goal is to use JQuery to display a menu of checkboxes based on a specific template in a div. To enhance user experience, I have included a search box that will filter the menu items. However, there is an unusual issue occurring. When the men ...

Take action once the Promise outside of the then block has been successfully completed

Presented below is the code snippet: function getPromise():Promise<any> { let p = new Promise<any>((resolve, reject) => { //some logical resolve(data); }); p.finally(()=>{ //I want do something when ou ...

No data received from XMLHttpRequest

Within my Java Web application, I'm making an ajax request using the following code: <script type="text/javascript"> function selectTableHandler() { console.log("inside selectTableHandler"); var xhttp = new XMLHttpRequest(); var se ...

selecting arrays within arrays according to their date values

With an array of 273 arrays, each containing data about a regular season NFL football game, I am looking to categorize the games by week. In total, there are 17 weeks in the NFL season that I want to represent using separate arrays. The format of my array ...

In JavaScript, merging objects will exclusively result in an identifier being returned

When working with mongoose, I have encountered an issue where combining data from multiple finds only displays the id instead of the entire object. Interestingly, when I use console.log() on the object directly, it shows all the contents. Below are snippe ...

Having trouble integrating SVG icons into my Angular project

I'm encountering an issue with adding icons that I've designed in Figma to my web application. Some of the icons are displaying correctly while others are not. Here is the code snippet I am using to add the icons: .dx-icon-info { mask-image: ...

Update the array state based on the selection of checkboxes and user input in real-time

In my current project using react js, I am working on a UI development task where I need to create a dynamic table based on data fetched from an API. Each row in the table includes a checkbox and a text input field that are dynamically generated. My goal i ...

Embedding anchor tags inside jQuery Ajax tabs

I'm currently working on creating a FAQ page and I am looking to utilize Ajax with jQuery tabs to load the content on a single page. Instead of having separate pages for each FAQ, I want to use anchors to load the content dynamically. How can I achie ...

Experiencing difficulties when trying to pan and zoom the data obtained from d3.json within a line chart

I am currently working on developing a trend component that can zoom and pan into data fetched using d3.json. Here is the code I have written so far: <script> var margin = { top: 20, right: 80, bottom: 20, left: 50 }, width = $("#trendc ...

Tips for Aligning Several Images Horizontally in an Article

Does anyone know how to center pictures in an article horizontally? I've tried various techniques without success. If it would be helpful, I can share the CSS I currently have. However, I am open to starting from scratch with the CSS as well since I ...