1

I would like to count with javascript the rows with a specific cell content.
This is the HTML code for the table:

<table class="table table-bordered" id="UsersDataTable">
    <thead>
      <tr>
        <th>H0</th>
        <th>H1</th>
        <th>H2</th>
        <th>H3</th>
        <th>H4</th>
        <th>H5</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>C0</td>
        <td>C1</td>
        <td>C2</td>
        <td>C3</td>
        <td>C4</td>
        <td><center><img src=".\pictures\green.jpg" class="img-circle" alt="Green" width="20" height="20"></center></td>
      </tr>
    </tbody>
</table>

How can I count all cells with 'alt="Green"'? Until now I have this code snippet:

function CountRows(TableID, alt) {
    var refTab = document.getElementById(TableID)
    var counter = 0
    for ( var i = 0; row = refTab.rows[i]; i++ ) {
        alert(refTab.rows[i].cells[5].innerHTML);
        if (refTab.rows[i].cells[5].innerHTML === alt)
        {
            counter ++
        }
    }

    return counter
}

Thanks for help
Patrick

1
  • Do you wish to count rows or cells? E.g. in one row can be more cells with specified attribute, or not? Commented Nov 29, 2016 at 0:03

3 Answers 3

2

You can use the document.querySelectorAll("#UsersDataTable [alt='Green']") to get what you want as follows. Then get the length of the returned array. That's your count.

var v=document.querySelectorAll("#UsersDataTable [alt='Green']");
console.log(v.length);
<table class="table table-bordered" id="UsersDataTable">
<thead>
  <tr>
    <th>H0</th>
    <th>H1</th>
    <th>H2</th>
    <th>H3</th>
    <th>H4</th>
    <th>H5</th>
  </tr>
</thead>
<tbody>
  <tr>
    <td>C0</td>
    <td>C1</td>
    <td>C2</td>
    <td>C3</td>
    <td>C4</td>
    <td><center><img src=".\pictures\green.jpg" class="img-circle" alt="Green" width="20" height="20"></center></td>
  </tr>
</tbody>

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

1 Comment

Thank you for the Response. I have some Troubles to insert the varaible 'TableID' and 'alt' into the snippet.
1

If you can use jQuery it can be done like this:

var allElementsWithAltAttribute = $("[alt=green]");

Or you can use document instead in pure JS:

var allElementsWithAltAttribute = document.querySelectorAll('[alt=green]');

And to get the count:

var count = allElementsWithAltAttribute.length;

1 Comment

The OP only wants the ones in rows, so the selector should be 'table [alt=Green]' or similar.
0

Your could check if there's any element with the passed alt using .querySelector('[alt="'+alt+'"]') so you code could be like :

if (refTab.rows[i].cells[5].querySelector('[alt="'+alt+'"]')!=null)
{
  counter++;
}

Hope this helps.

function CountRows(TableID, alt) {
  var refTab = document.getElementById(TableID)
  var counter = 0
  for ( var i = 0; row = refTab.rows[i]; i++ ) {
    if (refTab.rows[i].cells[5].querySelector('[alt="'+alt+'"]')!=null)
    {
      counter++;
    }
  }

  return counter;
}

alert( CountRows('UsersDataTable','Green') );
<table class="table table-bordered" id="UsersDataTable">
  <thead>
    <tr>
      <th>H0</th>
      <th>H1</th>
      <th>H2</th>
      <th>H3</th>
      <th>H4</th>
      <th>H5</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>C0</td>
      <td>C1</td>
      <td>C2</td>
      <td>C3</td>
      <td>C4</td>
      <td><center><img src=".\pictures\green.jpg" class="img-circle" alt="Green" width="20" height="20"></center></td>
    </tr>

    <tr>
      <td>C0</td>
      <td>C1</td>
      <td>C2</td>
      <td>C3</td>
      <td>C4</td>
      <td>C5</td>
    </tr>

    <tr>
      <td>C0</td>
      <td>C1</td>
      <td>C2</td>
      <td>C3</td>
      <td>C4</td>
      <td><center><img src=".\pictures\green.jpg" class="img-circle" alt="Green" width="20" height="20"></center></td>
    </tr>

    <tr>
      <td>C0</td>
      <td>C1</td>
      <td>C2</td>
      <td>C3</td>
      <td>C4</td>
      <td><center><img src=".\pictures\green.jpg" class="img-circle" alt="Green" width="20" height="20"></center></td>
    </tr>
  </tbody>
</table>

Comments

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.