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

152
Vistas
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 Respuestas
Responde la pregunta

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 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