Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

200
Views
change details icon when clicked but only for that details dropdown

I have the following code

// Fetch all the details element.
const details = document.querySelectorAll('details');

// Add the onclick listeners.
details.forEach((targetDetail) => {
  targetDetail.addEventListener('click', () => {
    // Close all the details that are not targetDetail.
    details.forEach((detail) => {
      if (detail !== targetDetail) {
        detail.removeAttribute('open');
      };

      // change menu items and menu collapse icons
      const icon = document.querySelector('.arrow i');

      if (icon.classList.contains('fa-chevron-down')) {
        icon.classList.remove('fa-chevron-down');
        icon.classList.add('fa-chevron-up');
      } else {
        icon.classList.remove('fa-chevron-up');
        icon.classList.add('fa-chevron-down');
      }
    });
  });
});
details > summary {
  cursor: pointer;
  list-style: none;
 }

details > summary::-webkit-details-marker {
  display: none;
}

details {
  transition: all 0.2s ease-in-out;
}

details[open] {
  display: block;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta2/css/all.min.css" rel="stylesheet"/>

<aside class="sidebar">
  <details>
    <summary>
      <i class="fas fa-users"></i>

      <span class="text hidden">Users</span>

      <span class="arrow">
        <i class="fas fa-chevron-down"></i>
      </span>
    </summary>

    <div>
      <a href="/dashboard/users/{{user._id}}">My profile</a>
    </div>
    <div>
      <a href="/dashboard/users/edit/{{user._id}}">Edit Profile</a>
    </div>
  </details>

  <details>
    <summary>
      <i class="fas fa-life-ring"></i>

      <span class="text hidden">Support</span>

      <span class="arrow">
        <i class="fas fa-chevron-down"></i>
      </span>
    </summary>

    <div>
      <a href="#">Tickets</a>
    </div>
  </details>
</aside>

When the user clicks a details dropdown I want the icon to change to chevron-up but this should also happen if I click on the second one and should reset the previous details icon to chevron-down.

So I want to loop though all the details as I'm going to be having the same icon, functionality for all dropdowns

Any help would be great.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!