Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

326
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!