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

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

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