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

155
Vistas
jsdom how to get an element inside another element inside foreach

i am new to JSDOM parser.

I have the following:

<div id='mydiv'>
  <ul>
    <li><a title='a'>TextA</a></li>
    <li><a title='b'>TextB</a></li>
    <li><a title='c'>TextC</a></li>
    <li><a title='d'>TextD</a></li>
  </ul>
</div>

I am using the following code but not able to get the text 'TextA', 'TextB', 'TextC', 'TextD'

const categoryDiv = dom.window.document.querySelectorAll('#mydiv > ul > li')
  .forEach(item => {
    console.log('item', item.getElement('a')); //not sure how to continue from here
  });
})
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

const categoryDiv = dom.window.document.querySelectorAll('#mydiv > ul > li')

After this first step you have a NodeList with the 4 list elements. With

console.dir(categoryDiv[0])

you can log the first list object to the console and see and expect all its properties. There are various ways to access the enclosed anchor tags. For example

  1. .children => HTML Collection
  2. .childNodes => NodeList
  3. .getElementsByTagName('a') => HTML Collection
  4. .querySelector('a') => href element

Only the last option gives you the link element directly, with the first three you have to select the first element in the selection to get to the link

For then accessing the text of the link there are again two options

  1. .innerHTML
  2. .textContent

In this case it doesn't matter which to choose because both give you the Text inside the link tags, if called on the link. If called on the list element it would look like this

listElem.outerHTML // <li><a title="a">TextA</a></li>
listElem.innerHTML  // <a title="a">TextA</a>
listElem.textContent // TextA

Sooo you actually don't have to access the link element. Simply call directly .textContent on the list items

Lastly you want to use .map instead of .forEach since .forEach only iterates, but doesn't return anything. The NodeList directly is not iterable with .map but can be easily converted with the spread operator

So all together for example like this

const categoryDiv = [...document.querySelectorAll('#mydiv > ul > li')]
  .map(listItem => listItem.textContent)

console.log(categoryDiv)  // ['TextA', 'TextB', 'TextC', 'TextD']

or this

const categoryDiv = Array.from(document.querySelectorAll('#mydiv > ul > li'), listItem => listItem.textContent)

Or a very fast way without even iterating would be

document.querySelector('#mydiv ul')
  .textContent  // 'TextA TextB TextC TextD'
  .split(' ')  // ['TextA', 'TextB', 'TextC', 'TextD']
about 4 years ago · Juan Pablo Isaza Denunciar

0

Simply modify your original code with getElementsByTagName and innerHTML:

const categoryDiv = dom.window.document.querySelectorAll('#mydiv > ul > li')
.forEach(item => {
   console.log('item -- ' + item.getElementsByTagName('a')[0].innerHTML); 
 });
})
about 4 years ago · Juan Pablo Isaza Denunciar

0

This could be as simple as:

  let targets = document.querySelectorAll('#mydiv > ul > li a');

  for (let target of targets) {
    console.log(target.text); 
  }
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