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

177
Vistas
Find a specific anchor link from a li to be removed

I tried to make a code to remove a li from ul but I would like to be more specific. I would like to find exact anchor link before removing it along with the li it contains.

In the example below, I want to remove only Another link.

My code so far looks like:

<ul class="working-list">    
        <li class="work color1">
            <a href="?c=Some link 1">
                <span class="title">
                    link 1</span>
            </a>
        </li>
    
        <li class="work color1">
            <a href="?c=someLink2"><span class="title">
                    Some link 2</span>
            </a>
        </li>
    
        <li class="work color1">
            <a href="?c=anotherLink"><span class="title">
                    Another link</span>
            </a>
        </li> 

        <li class="work color1">
            <a href="?c=SmallLink"><span class="title">
                    Small link</span>
            </a>
        </li>    
    </ul>

And the script so far:

let pickEl = document.querySelectorAll('.working-list li');
    // let pickA = document.querySelector('a[href=\'?c=anotherLink\']');
    console.log(pickEl);
    pickEl[3].remove();

What would be the best approach?

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

0

You can try using Attribute selectors selector:

let pickEl = document.querySelector('.working-list li a[href="?c=anotherLink"]');
console.log(pickEl);
//remove the parent li
pickEl.closest('li').remove();
<ul class="working-list">    
    <li class="work color1">
        <a href="?c=Some link 1">
          <span class="title">link 1</span>
        </a>
    </li>

    <li class="work color1">
        <a href="?c=someLink2">
          <span class="title">Some link 2</span>
        </a>
    </li>

    <li class="work color1">
        <a href="?c=anotherLink">
          <span class="title">Another link</span>
        </a>
    </li> 

    <li class="work color1">
        <a href="?c=SmallLink">
          <span class="title">Small link</span>
        </a>
    </li>    
</ul>

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can forEach the pickEl then see textContent like:

let pickEl = document.querySelectorAll('.working-list li');
const deleteText = 'Another link';
pickEl.forEach(el =>{
  const a = el.querySelector('a');
  if(a.textContent.trim() === deleteText){
    el.remove();
  }
});
<ul class="working-list">
  <li class="work color1">
    <a href="?c=Some link 1">
      <span class="title">
                    link 1</span>
    </a>
  </li>

  <li class="work color1">
    <a href="?c=someLink2"><span class="title">
                    Some link 2</span>
            </a>
  </li>

  <li class="work color1">
    <a href="?c=anotherLink"><span class="title">
                    Another link</span>
            </a>
  </li>

  <li class="work color1">
    <a href="?c=SmallLink"><span class="title">
                    Small link</span>
            </a>
  </li>
  </ul>

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