Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

238
Visualizações
How to get a collection of all child elements that have parents with ID attributes?

I have a situation where I need to collect a collection of elements that have parents with IDs. So if

My scenario


<table>
 <thead></tead>
 <tbody>
   <tr id="row1"><td><div class="pro_fname">name</div></td></tr>
   <tr id="row2"><td><div class="pro_fname">name</div></td></tr>
   <tr id="row3"><td><div class="pro_fname">name</div></td></tr>
   <tr><td><div class="pro_fname">name</div></td></tr>
   <tr><td><div class="pro_fname">name</div></td></tr>
   <tr><td><div class="pro_fname">name</div></td></tr>
  </tbody>
</table>

From JavaScript, how can I guarantee to store the first three fname class elements into a list, but disregard the other fname class elements, all because the first 3 have parent ID's on elements?

Below is my initial attempt...


//collect all desired elements
var objFirstNames = document.getElementsByClassName("pro_fname");
...
...

//call function to check against the array type objects collected. arguments will grow...
//lets just focus on first names
checkInputValuesObjects(objFirstNames, objLastNames, objAttEmails, ...);

//filter values where parent ID's exist
function checkInputValuesObjects() {
  for (var i = 0; i < arguments.length; i++) {
     if (arguments[i][0].parentNode.parentNode.id.length) {
       //then I can do something with these specific elements...
       console.log(arguments[i]);
    }
  }
}

...but I'm having a hard time, specifically through Firefox Console, if I am truly capturing only 3 objects, and not all 6....

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

document.querySelectorAll('tr[id] div');

Will first select all tr with an id attribute, then get all the div elements inside.

const ee = document.querySelectorAll('tr[id] div');
ee.forEach(e => console.log(e.className));
<table>
 <thead></tead>
 <tbody>
   <tr id="row1"><td><div class="pro_fname">name</div></td></tr>
   <tr id="row2"><td><div class="pro_fname">name</div></td></tr>
   <tr id="row3"><td><div class="pro_fname">name</div></td></tr>
   <tr><td><div class="pro_fname">name</div></td></tr>
   <tr><td><div class="pro_fname">name</div></td></tr>
   <tr><td><div class="pro_fname">name</div></td></tr>
  </tbody>
</table>

The above will yield:

pro_fname
pro_fname
pro_fname
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda