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

352
Vistas
jQuery addClass to a child element not working in Egde

I have this structure:

<ul>
  <li class="js-drilldown-back"><a>Back</a></li>
  <li><a href="#">item</a></li>
  <li><a href="#">item</a></li>
  <li><a href="#">item</a></li>
</ul>

and I'm trying to add a class to the first tag, so it becomes this:

<ul>
  <li class="js-drilldown-back"><a class="click-back">Back<a></li>
  <li><a href="#">item</a></li>
  <li><a href="#">item</a></li>
  <li><a href="#">item</a></li>
</ul>

The framework I use is adding this back button dynamically, so I can't manually add the class to it. I have tried this code:

$(".js-drilldown-back").children("a").addClass("click-back")

but it doesn't seem to work in Edge, other browsers seem happy with it. I've also tried:

$(".js-drilldown-back:has a").addClass("click-back")
$(".js-drilldown-back:has > a").addClass("click-back")
$(".js-drilldown-back a").addClass("click-back")
$(".js-drilldown-back > a").addClass("click-back")

What I'm trying to achieve is when you click the Back button it removes a class from the body tag, ie:

$(".click-back").click(function(){
  $("#page").removeClass("submenu-open")
})

Targetting the parent (.js-drilldown-back) doesn't seem to work which is why I'm trying to add the class to the child and target this instead.

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

0

You don't appear to have proper semicolon terminations after each statement.

$(".js-drilldown-back").children("a").addClass("click-back");

:first-child seems to work fine in Edge, or maybe the console.log in jsfiddle is being lenient? I also reworked how you're applying your on click event and confirmed that it removes the class from the anchor.

$(".js-drilldown-back:first-child").addClass("click-back");
console.log( $(".js-drilldown-back:first-child").hasClass("click-back") );

$(document).on("click", ".click-back", function(){
  $(this).removeClass("click-back");
})

Console confirms that the anchor has the desired class, i.e., :first-child is working.

Console confirms that the anchor has the desired class

Upon clicking the anchor, source confirms that the class is removed. I changed the target just to confirm it was removing something I could see and verify.

Upon clicking the anchor, source confirms that the class is removed

All tested in Edge here: https://jsfiddle.net/8bfo9cza/

over 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
Asistente virtual