Advertisement
Threed90

Generate Report

Jan 25th, 2022
532
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function generateReport() {
  2.     let checkboxElements = document.querySelectorAll('input[type="checkbox"]');
  3.     let checkedElements = [];
  4.  
  5.     for (let i = 0; i < checkboxElements.length; i++) {
  6.         if(checkboxElements[i].checked == true){
  7.             checkedElements.push([checkboxElements[i].name, i]);
  8.         }
  9.     }
  10.  
  11.     let arr = [];
  12.  
  13.     let trElements = document.querySelectorAll('tbody tr');
  14.  
  15.     for (let i = 0; i < trElements.length; i++) {
  16.         let obj = {};
  17.        
  18.         for (let j = 0; j < checkedElements.length; j++) {
  19.             obj[checkedElements[j][0]] = trElements[i].cells[checkedElements[j][1]].textContent;
  20.         }
  21.  
  22.         arr.push(obj);
  23.     }
  24.  
  25.     let json = JSON.stringify(arr, null, 4);
  26.     if(checkedElements.length){
  27.         document.getElementById('output').textContent = json;
  28.     } else{
  29.         document.getElementById('output').textContent = "";
  30.     }
  31.    
  32. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement