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

202
Views
How to get adjacent col value in a div-table using js?

I have an html table, which is structured like this:

<body>
<div class="block div-table" id="sidebar-record-block">
<div class="div-table-row">
  <div class="div-table-header">Sel</div>
  <div class="div-table-header">Color</div>
  <div class="div-table-header">Hex</div>
</div>
<div id="field-0000" class="div-table-row">
  <input type="checkbox" class="div-table-td" name="checkBox" id="cbfield-0000">
  <div class="div-table-td">yellow</div>
  <div class="div-table-td"></div>
</div>
  <div id="field-0001" class="div-table-row">
    <input type="checkbox" class="div-table-td" name="checkBox" id="cbfield-0001">
    <div class="div-table-td">red</div>
  <div class="div-table-td"></div>
</div>
</body>

I can iterate over the checkboxes using the code below and push the checked rows into an array:

saveButton.onclick = function(){
   var checkedRowIndexes = []; 
  var selectedColors = []; 
  var checkedRows = document.querySelectorAll("input[type='checkbox']");
  for (var i = 0; i < checkedRows.length; i++){
    if (checkedRows[i].checked){
    checkedRowIndexes.push(i);
    }
  }
  console.log(checkedRowIndexes);
}

But how would I go about iterating over the table and push the color (2º col) instead, using javascript?

Thank you!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

var checkedRowIndexes = [];
var selectedColors = [];
var checkedRows = document.querySelectorAll("input[type='checkbox']");

for (var i = 0; i < checkedRows.length; i++) {
  if (checkedRows[i].checked) {    
    checkedRowIndexes.push(i);
    selectedColors.push(checkedRows[i].nextElementSibling.innerText);
  }
}
console.log(checkedRowIndexes, selectedColors);
<div class="block div-table" id="sidebar-record-block">
  <div class="div-table-row">
    <div class="div-table-header">Sel</div>
    <div class="div-table-header">Color</div>
    <div class="div-table-header">Hex</div>
  </div>
  <div id="field-0000" class="div-table-row">
    <input type="checkbox" class="div-table-td" name="checkBox" id="cbfield-0000">
    <div class="div-table-td">yellow</div>
    <div class="div-table-td"></div>
  </div>
  <div id="field-0001" class="div-table-row">
    <input type="checkbox" class="div-table-td" name="checkBox" id="cbfield-0001">
    <div class="div-table-td">red</div>
    <div class="div-table-td"></div>
  </div>

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!