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

332
Vistas
JavaScript - Using QuerySelector on multi-nested lists

If I have a nested list like this:

<div class="nav">
    <ul>
        <li><a href="#number-one">Number 1</a></li>
        <li><a href="#number-two">Number 2</a></li>
        <ul>
            <li><a href="#number-three">Number 3</a></li>
            <li><a href="#number-four">Number 4</a></li>
        </ul>
        <li><a href="#number-five">Number 5</a></li>
    </ul>
</div>

I want to be able to get the href values from every one of these links using querySelector.

Something like this only returns the links in the top level :

const getNav = document.querySelector('.nav li');

How can I get all of the href values from every link in a nested list?

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

0

In one line with querySelectorAll & map :

[...document.querySelectorAll('.nav li a')].map(({href})=>href)

Gives result:

['https://stackoverflow.com/questions/72943233/javas…ng-queryselector-on-multi-nested-lists#number-one', 'https://stackoverflow.com/questions/72943233/javas…ng-queryselector-on-multi-nested-lists#number-two', 'https://stackoverflow.com/questions/72943233/javas…-queryselector-on-multi-nested-lists#number-three', 'https://stackoverflow.com/questions/72943233/javas…g-queryselector-on-multi-nested-lists#number-four', 'https://stackoverflow.com/questions/72943233/javas…g-queryselector-on-multi-nested-lists#number-five']

live example

console.log([...document.querySelectorAll('.nav li a')].map(({href})=>href))
<div class="nav">
    <ul>
        <li><a href="#number-one">Number 1</a></li>
        <li><a href="#number-two">Number 2</a></li>
        <ul>
            <li><a href="#number-three">Number 3</a></li>
            <li><a href="#number-four">Number 4</a></li>
        </ul>
        <li><a href="#number-five">Number 5</a></li>
    </ul>
</div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

querySelector only returns one element, in this case you will get the first li element after the nav class. To get all li you can use: querySelectorAll - as it return an array matching your select options. MDN DOCS

To get an attribute you'll use: element.getAttribute('href')

MDN DOCS

Example:

let elements = document.querySelectorAll('.nav li a')
elements.forEach(el => { 
 // logic goes here 
 let href = el.getAttribute('href')
})
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use this code to get every href values.

const getNav = document.querySelectorAll('.nav li a');
   
    getNav.forEach(element=>{
        console.log(element.getAttribute("href"))
    })

querySelectorAll gets all of the a elements that are in .nav li then by a foreach loop you'll be able to get all href values of them.

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