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

294
Vistas
For each div that has li, add class

I'm using jQuery to achieve the following:

  • For each div with the same id, check to see if the div has any li elements. If it does, then add a specific class.

The code below only works for one of the divs. What do I change so that it loops through all instances of div with the same id.

$('#general-block').each(function() {
   if ($('general-block:has(li)').length) {
      $('general-block').addClass('testing-block');
   }
});

// The class 'testing-block' should be added to the first two divs with the id 'general-block' as they have li elements.
<div id='general-block' class='block'>
   <ul>
      <li>Test</li>
      <li>Test</li>
   </ul>
</div>

<div id='general-block' class='block'>
   <ul>
      <li>Test</li>
      <li>Test</li>
   </ul>
</div>

<div id='general-block' class='block'>
   <p>Class won't apply to the parent div</p>
</div>
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You can't have multiple elements with the same id,ID must be Unique

you can use .each via class , and Add Custom Class for any div has li

$('.general-block').each(function() {
   if ($(this).children().find('li').length>0) {
      $(this).addClass('testing-block');
   }
});



    <div class='general-block block'>
   <ul>
      <li>Test</li>
      <li>Test</li>
   </ul>
</div>

<div class='general-block block'>
   <ul>
      <li>Test</li>
      <li>Test</li>
   </ul>
</div>

<div  class='general-block block'>
   <p>Class won't apply to the parent div</p>
</div>
about 4 years ago · Juan Pablo Isaza Denunciar

0

As was specified previously, you can't have several elements with the same id, ID must be unique.

However, it is totally possible to add a class if the element has a specified child.

$(".block:has(li)").addClass('testing-block');

A class selector will return all of the matches. The Id just the first one. All elements matches is what would work nicely here.

But again for unique elements use different IDs.

about 4 years ago · Juan Pablo Isaza Denunciar

0

$('#general-block > ul').each(function() {
   if ($('general-block:has(li)').length) {
      $('general-block').addClass('testing-block');
   }
});

also ID must be unique

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