Home > Blockchain >  I need to display that how many '1' values have 3rd and 4th columns
I need to display that how many '1' values have 3rd and 4th columns

Time:08-24

Since I am new in Web Development sorry for that. My HTML table has 4 rows and i need to find how many 1 values in my 3rd and 4th columns. 3rd and 4th columns of table consists of 1's and 0's.

Html code that i need to find '1' values in checkOne classes.

<th><input type="text" id="filter0" placeholder="name"></th>
<th><input type="text" id="filter1" placeholder="Date"></th>
<th><input type="text"  id="filter2" placeholder="Line1"></th>
<th><input type="text"  id="filter3" placeholder="Line2"></th>

JavaScript code that i use display for number of rows


function rowCount(){ 
      var a = document.getElementById("filter");
      var rows = a.rows.length - 1;
      alert("Total Rows: "   rows);

       //alert(line1 has ... 1 values)
        //alert(line2 has ... 1 values)
}  

CodePudding user response:

You can go through each of the rows adding together the numbers in the 3rd and 4th cell in each row.

Note that rows as you have defined it is a live collection of elements. It is not an array as such so you have to first turn it into an array to use forEach on it.

Here's a very simple snippet demonstrating this:

function rowCount() {
  const rows = document.getElementById("filter").rows;
  let valuesCount = 0;
  [...rows].forEach(row => { // uses the spread function to create an array
    const n3 = Number(row.querySelector(':nth-child(3)').innerHTML);
    const n4 = Number(row.querySelector(':nth-child(4)').innerHTML);
    if (n3 === 1) valuesCount  ;
    if (n4 === 1) valuesCount  ;
  })
  return valuesCount;
}
alert(rowCount());
<table id="filter">
  <tr>
    <td>a</td>
    <td>b</td>
    <td>1</td>
    <td>0</td>
  </tr>
</table>

CodePudding user response:

Something like this should help

function rowCount(){ 
  const rows = document.getElementById("filter").rows;
  let valuesCount = 0;
  Array.from(rows).forEach(row => {
    if (row.cells[2].innerHtml === 1 || row.cells[3].innerHtml === 1) valuesCount  
  })
  return valuesCount
}  
  • Related