0

How can I count td in each row, It should say row 1 have two td and row 3 have one td. I need to count td per row(tr)

$(function() {
  $('.Create-New-Order').click(function() {
    var total = $('#mytbl td').length;
    alert('tr count = ' + total);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

<table border="1px solid red">
  <tr>
    <th>Name</th>
    <th>Email</th>
  </tr>
  <tbody id="mytbl">
    <tr>
      <td>sfdsd</td>
      <td>[email protected]</td>
    </tr>
    <tr>
      <td>sfdsd</td>
      <td>[email protected]</td>
    </tr>
    <tr>
      <td>sfdsd</td>
    </tr>
  </tbody>
</table>
<br>
<br>
<a href="#" class="Create-New-Order">Create-New-Order</a>

7
  • What have you tried so far. Please share your code. What do you want to do with the counts? Have you tried Google for tr and td select? Commented Dec 10, 2019 at 19:49
  • um, select them and read length? Commented Dec 10, 2019 at 19:51
  • Does this answer your question? jQuery: count number of td in a table row Commented Dec 10, 2019 at 19:58
  • @devlincarnate its just counting rows not, cells in each row Commented Dec 10, 2019 at 20:01
  • @developer - I updated it Commented Dec 10, 2019 at 20:03

2 Answers 2

2

You need to loop over each row and then get the number of cells with $(this).find('td').length:

$('.Create-New-Order').click(function() {
  var total = $('#mytbl tr').each(function() {
    console.log($(this).find('td').length)
  })
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

<table border="1px solid red">
  <tr>
    <th>Name</th>
    <th>Email</th>
  </tr>
  <tbody id="mytbl">
    <tr>
      <td>sfdsd</td>
      <td>[email protected]</td>
    </tr>
    <tr>
      <td>sfdsd</td>
      <td>[email protected]</td>
    </tr>
    <tr>
      <td>sfdsd</td>
    </tr>
  </tbody>
</table>
<br>
<br>
<a href="#" class="Create-New-Order">Create-New-Order</a>

Sign up to request clarification or add additional context in comments.

Comments

1

This is easy first get all rows the loop throw each and count the td.

See below

    $(function() {
      $('.Create-New-Order').click(function() {
        var trs=document.querySelectorAll("#mytbl tr")
        trs.forEach(function(tr){
          console.log(tr.querySelectorAll("td").length)
        })

        
      });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

    <table border="1px solid red">
      <tr>
        <th>Name</th>
        <th>Email</th>
      </tr>
      <tbody id="mytbl">
        <tr>
          <td>sfdsd</td>
          <td>[email protected]</td>
        </tr>
        <tr>
          <td>sfdsd</td>
          <td>[email protected]</td>
        </tr>
        <tr>
          <td>sfdsd</td>
        </tr>
      </tbody>
    </table>
    <br>
    <br>
    <a href="#" class="Create-New-Order">Create-New-Order</a>

3 Comments

Any idea how can we do same in pure js?
Yes will change it for you
See upp did it with pure js

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.