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

150
Views
Why my console log is returning HTMLCollection0?

I trying to return that text inside of the list but instead Im getting htmlcollection0

my JS:

 Array.from(document.getElementById("main-menu").getElementsByTagName("li")).forEach(
        (x) =>
          (x.onclick = () =>
            console.log({
              event_name: "menu item clicked",
              menu_item: x.getElementsByTagName("li"),
            }))
      );

my html:

<div id="main-body">
      <ol id="main-menu">
        <li class="selected">Shop Spirits</li>
        <li>Shop Bourbon</li>
        <li>Shop Scotch</li>
        <li>Shop Rum</li>
      </ol>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

x.getElementsByTagName('li') returns all the li elements nested inside x. There aren't any, so that returns an empty NodeList.

If you want to log the text of the clicked item, use x.innerText, there's no need to search for anything.

Array.from(document.getElementById("main-menu").getElementsByTagName("li")).forEach(
  (x) =>
  (x.onclick = () =>
    console.log({
      event_name: "menu item clicked",
      menu_item: x.innerText
    }))
);
<div id="main-body">
  <ol id="main-menu">
    <li class="selected">Shop Spirits</li>
    <li>Shop Bourbon</li>
    <li>Shop Scotch</li>
    <li>Shop Rum</li>
  </ol>

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!