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

93
Vistas
Linking multiple hrefs to accordion and opening the corresponding section - Jquery

I have multiple hrefs that need to link to the proper accordion at the bottom of the page. The code I have now works, however I'm trying to minimize the amount of times I repeat functions as a coworker of mine points out that they are impractical. I have tried the location.hash solution, but it doesnt work for me. Any ideas would be appreciated.

window.contentAssetInitFunctions.push(function() {
  $(document).ready(function() {
    $("#start").click(function() {
      $('#startExchange, #startHeader').addClass('active');
      $('#packingSlip, #packingHeader').removeClass('active');
    });
    $("#pack").click(function() {
      $('#packingSlip, #packingHeader').addClass('active');
      $('#startExchange, #startHeader').removeClass('active');
    });
  });
});
<a href="#startHeader" id="start" class="text-link">Learn How</a>
<a href="#packingHeader" id="pack" class="text-link">Learn How</a>

<a class="question" id="startHeader"> EXCHANGE</a>
<div class="answer" id="startExchange">
  content
</div>

<a class="question" id="packingHeader">HOW TO START AN EXCHANGE WITH A PACKING SLIP</a>
<div class="answer" id="packingSlip">
  content
</div>

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

If you wrap each question/answer with a container tag (like an article or div), then you can target the whole section instead of having to pick out the question and answer with their custom ids.

Below you'll see I added a wrapper article tag with an id that describes the section. Then the javascript removes all active tags, and then grabs the id from the href, finds that article and adds an active tag to that one.

Something like this:

$(document).ready(function () {
  $(".text-link").click(function () {
    let section = $(this).attr('href').substr(1);
    $('article .question, article .answer').removeClass('active')
    $(`#${section} .question, #${section} .answer`).addClass('active')
  });
});
<a href="#start" class="text-link">Learn How</a>
<a href="#packing" class="text-link">Learn How</a>

<article id="start">
  <a class="question"> EXCHANGE</a>
  <div class="answer">
    content
  </div>
</article>

<article id="packing">
  <a class="question">HOW TO START AN EXCHANGE WITH A PACKING SLIP</a>
  <div class="answer">
    content
  </div>
</article>

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