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

171
Views
jQuery/Cheerio: How to recursively get certain elements by name/tag?

I'm trying to make a bot that scrapes links from a website. I am running in to some weird error that I cannot figure out. My guess is that the second if statement is failing to check and also my unfamiliarity with jQuery is not helping.

Error:

element.each is not a function

const $ = load(html);
const html = $("#id");

const temp = [];

function recursive(element) {
  if (element.name === "a") {
    temp.push(element);
  }
  
  if (!element || element.children().length > 0 === false) {
    return "DID NOT FIND IT OR NO CHILDREN FOUND";
  }

  return element.each((_, item) => recursive(item));
}

recursive(html);
return temp;
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I've tried to create simple snippet demonstrating what you seem to accomplished with JQuery.

Firstly, your check if for the Tag of an element doesn't seemed to be working properly. I had to use the .prop('tagName') to get the Tag of the element. And it gets returned in all capital letters.

Your second IF-Statement should work fine, but the .each() Method didnt work as expected. You want to iterate through all children and start the recursive function. And the way you provided the child element didnt end up working. The .each() Method want a callback function which provides two parameters as you have uses correctly. but the Item is a normal HTML Node and you had to select it with the JQuery Constant $ like $(item). This gives you the desired JQuery Element you can work with.

const html = $("#test");

const temp = [];

function recursive(element) {
  if (element.prop("tagName") === "A") {
    temp.push(element);
  }
  if (!element || element.children().length > 0 === false) {
    return "DID NOT FIND IT OR NO CHILDREN FOUND";
  }

  return element.children().each((i, item) => recursive($(item)));
}

recursive(html);
console.log(temp)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="test">
  <div class="headings">
    <h1>Heading</h1>
    <a href="./main.html">Main Page</a>
  </div>
  <div class="test-cls">
    <button>Hello</button>
    <a href="./test.html">Test Page</a>
  </div>
</div>

about 4 years ago · Santiago Trujillo 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!