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

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

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 Report

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 Report

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