Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

148
Vistas
Add css class to menu item based on current url

I have menu and I want to add css class to menu item based on current url

I try to use jQuery but this code add current-active-menu css class only to a href="https://mywebsite.com/dashboard/ element

jQuery(document).ready(function() {
  if (window.location.href.indexOf("https://mywebsite.com/dashboard/") > -1) {
    $('a[href="https://mywebsite.com/dashboard/"]').addClass("current-active-menu");
  }
});

jQuery(document).ready(function() {
  if (window.location.href.indexOf("https://mywebsite.com/dashboard/?candidate-page=resumes-list") > -1) {
    $('a[href="https://mywebsite.com/dashboard/?candidate-page=resumes-list"]').addClass("current-active-menu");
  }
});

jQuery(document).ready(function() {
  if (window.location.href.indexOf("https://mywebsite.com/dashboard/?candidate-page=my-orders") > -1) {
    $('a[href="https://mywebsite.com/dashboard/?candidate-page=my-orders"]').addClass("current-active-menu");
  }
});
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You should use an else if and put https://mywebsite.com/dashboard/ at the end because it would match your other 2:

jQuery(document).ready(function() {
  if (window.location.href.indexOf("https://mywebsite.com/dashboard/?candidate-page=my-orders") > -1) {
    $('a[href="https://mywebsite.com/dashboard/?candidate-page=my-orders"]').addClass("current-active-menu");
  } else if (window.location.href.indexOf("https://mywebsite.com/dashboard/?candidate-page=resumes-list") > -1) {
    $('a[href="https://mywebsite.com/dashboard/?candidate-page=resumes-list"]').addClass("current-active-menu");
  } else if (window.location.href.indexOf("https://mywebsite.com/dashboard/") > -1) {
    $('a[href="https://mywebsite.com/dashboard/"]').addClass("current-active-menu");
  }
});

Also it looks like you whole url should match so you should think about using === in your if instead of indexOf:

window.location.href === "https://mywebsite.com/dashboard/?candidate-page=my-orders"

With your original problem, using indexOf put the active class onto the wrong link because you were testing if that string exists anywhere in the url (which https://mywebsite.com/dashboard/ exists in all three of your urls you test)

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda