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

49
Views
Assign class to link element where href is equal to a specific value using jquery

How do I assign class="active" to a link element when another link is clicked?

i.e I have:

<div id="wrap">
   <a href="one.php"></a>
   <a href="two.php"></a>
   <a href="three.php"></a>
</div>

<a href="one.php"></a>
<a href="two.php"></a>
<a href="three.php"></a>

But when the the link for one.php is clicked below, I want the link in the div wrap above to have the active class, as so:

<div id="wrap">
   <a href="one.php" class="active"></a>
   <a href="two.php"></a>
   <a href="three.php"></a>
</div>

<a href="one.php"></a>
<a href="two.php"></a>
<a href="three.php"></a>

Basically I was thinking something like:

var foo = $(this).attr("href");
if ($(this).attr("href") == "hello.php")
   $("#wrap").//something else
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Add a click event listener to every anchor element that adds the active class to every anchor, then removes the active class on the anchor element clicked.

$('a').click(function(){
  $('a').addClass('active');
  $(this).removeClass('active');
})
.active{
  background-color: yellow;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a href="#">link 1</a>

<a href="#">link 2</a>

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!