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

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

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