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

117
Views
Add active after click link

I have this code below, and I wanted to know how I can make it active after the click?

.sprite {
  background: url(https://liveplanet.app.br/assets/images/sprite-icons.png) no-repeat;
  width: 40px;
  height: 40px;
  display: inline-block;
}

a.audio {
  background-position: 0px -720px;
}

a.audio:hover {
  background-position: -40px -720px;
}

a.audio:active {
  background-position: -80px -720px;
}
<li class="list-group-item">
  <a (click)="toggleAudio()" href="javascript:void(0);" class="sprite audio" data-bs-toggle="tooltip" data-bs-placement="top" title="Audio"></a>
</li>

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

As per the spec, the :active CSS selector will only match

while an element is being activated by the user

What you're looking for is the :focus pseudo selector, which also matches after your click (without any additional JavaScript).

a.audio:focus { ... }

If you want to control the active element (document.activeElement) with JavaScript, you can use the focus(). The CSS selector :active will not match.

<li class="list-group-item">
  <!-- adapt click handler to current JavaScript framework -->
  <a (click)="toggleAudio(); this.focus();" href="javascript:void(0);" class="sprite audio" data-bs-toggle="tooltip" data-bs-placement="top" title="Audio"></a>
</li>

An alternative approach to display that a link has been clicked, is to toggle a CSS separate class, with element.classList.toggle().

<style>
  a.active { ... }
</style>

<li class="list-group-item">
  <!-- adapt click handler to current JavaScript framework -->
  <a (click)="toggleAudio(); this.classList.toggle('active');" href="javascript:void(0);" class="sprite audio" data-bs-toggle="tooltip" data-bs-placement="top" title="Audio"></a>
</li>
about 4 years ago · Juan Pablo Isaza Report

0

So If I understand you correctly, then you want this button (a button for controlling the audio) to have a active class when clicked on correct? If so here is my proposal. Hopefully this can provide what you are looking to achieve. Let me know if this works for you or not.

You can see that when clicking on the button, if you look at it through inspect element. It will append a 'active' class on click, & remove it when clicked again.

Codepen here: https://codepen.io/no-named-user/pen/abELyar

let a = document.querySelector('#active');

a.onclick = function() {
  a.classList.toggle("active");
}
.sprite {
  background: url(https://liveplanet.app.br/assets/images/sprite-icons.png) no-repeat;
  width: 40px;
  height: 40px;
  display: inline-block;
}

a.audio {
  background-position: 0px -720px;
}

a.audio:hover {
  background-position: -40px -720px;
}

a.audio:active {
  background-position: -80px -720px;
}
<li class="list-group-item">
  <a (click)="toggleAudio()" href="javascript:void(0);" class="sprite audio" data-bs-toggle="tooltip" data-bs-placement="top" title="Audio" id = "active"></a>
</li>

about 4 years ago · Juan Pablo Isaza Report
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!