PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, August 3, 2022

[FIXED] How to get adjacent col value in a div-table using js?

 August 03, 2022     html, html-table, javascript     No comments   

Issue

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!


Solution

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>



Answered By - traynor
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing