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

161
Views
¿Cómo agregar una clase para la URL ACTIVA en la barra lateral?

Mi código:

 const address = location.pathname; const links = document.querySelectorAll('.sidebarMain a'); links.forEach(function(el) { if(new RegExp(`^${el.getAttribute('href')}(.*)$`, 'i').test(address)) el.classList.add('active')});

Pero no funciona, ¿dónde me equivoqué?

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

0

El HREF de los enlaces incluirá automáticamente el dominio actual al acceder a través de javascript, por lo que puede compararlo directamente con window.location.href

 let links = document.querySelectorAll(".sidebarMain a"); let cur = window.location.href; console.log(cur) links.forEach(function(link){ if(link.getAttribute("href") == cur){ link.classList.add("active"); } });
 .active{font-weight:bold;color:red;}
 <div class="sidebarMain"> <a href="https://stacksnippets.net/js">Active</a> <a href="aaa">test3</a> </div>

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!