JS loop through table and print text in column based on condition





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I have a simple table, and I need to print a text in a certain column based on condition over hidden columns. There are multiple rows as a record which have same TD ID. What I need to do is, to go through each row of table and check status1 (gv-field-15-139), status2 (gv-field-15-140) and status3 (gv-field-15-141). If one of this status (gv-field-15-150) is 'Completed' I want to print Completed in Status column. is it possible?






 $(document).ready(function(){
if(($('td#gv-field-15-139').text() == 'Completed') || ($('td#gv-field-15-
140').text() == 'Completed') || ($('td#gv-field-15-141').text() == 'Completed') ) {
$("td#gv-field-15-150").text('Completed');
}

    <table>
<tr>
<th>Student Name</th>
<th>Nationality</th>
<th>Status</th>
<th>Status1</th>
<th>Status2</th>
<th>Status3</th>
</tr>
<tr>
<td id="gv-field-15-1">Student A</td>
<td id="gv-field-15-90">India</td>
<td id="gv-field-15-150">&nbsp;</td>
<td id="gv-field-15-139">Completed</td>
<td id="gv-field-15-140">Pending</td>
<td id="gv-field-15-141">Pending</td>
</tr>
<tr>
<td id="gv-field-15-1">Student B</td>
<td id="gv-field-15-90">China</td>
<td id="gv-field-15-150">&nbsp;</td>
<td id="gv-field-15-139">Completed</td>
<td id="gv-field-15-140">Pending</td>
<td id="gv-field-15-141">Pending</td>
</tr>
</table>





This only works for 1 row and when it becomes 2 row or more it doesn't work. Could you please show me a way to achieve this.










share|improve this question

























  • Do not duplicate ids, that's why you can't access past the first one. When JS finds an id, it naturally assumes that's the only tag with that id. Change your ids into classes.

    – zer00ne
    Jan 3 at 9:31




















1















I have a simple table, and I need to print a text in a certain column based on condition over hidden columns. There are multiple rows as a record which have same TD ID. What I need to do is, to go through each row of table and check status1 (gv-field-15-139), status2 (gv-field-15-140) and status3 (gv-field-15-141). If one of this status (gv-field-15-150) is 'Completed' I want to print Completed in Status column. is it possible?






 $(document).ready(function(){
if(($('td#gv-field-15-139').text() == 'Completed') || ($('td#gv-field-15-
140').text() == 'Completed') || ($('td#gv-field-15-141').text() == 'Completed') ) {
$("td#gv-field-15-150").text('Completed');
}

    <table>
<tr>
<th>Student Name</th>
<th>Nationality</th>
<th>Status</th>
<th>Status1</th>
<th>Status2</th>
<th>Status3</th>
</tr>
<tr>
<td id="gv-field-15-1">Student A</td>
<td id="gv-field-15-90">India</td>
<td id="gv-field-15-150">&nbsp;</td>
<td id="gv-field-15-139">Completed</td>
<td id="gv-field-15-140">Pending</td>
<td id="gv-field-15-141">Pending</td>
</tr>
<tr>
<td id="gv-field-15-1">Student B</td>
<td id="gv-field-15-90">China</td>
<td id="gv-field-15-150">&nbsp;</td>
<td id="gv-field-15-139">Completed</td>
<td id="gv-field-15-140">Pending</td>
<td id="gv-field-15-141">Pending</td>
</tr>
</table>





This only works for 1 row and when it becomes 2 row or more it doesn't work. Could you please show me a way to achieve this.










share|improve this question

























  • Do not duplicate ids, that's why you can't access past the first one. When JS finds an id, it naturally assumes that's the only tag with that id. Change your ids into classes.

    – zer00ne
    Jan 3 at 9:31
















1












1








1








I have a simple table, and I need to print a text in a certain column based on condition over hidden columns. There are multiple rows as a record which have same TD ID. What I need to do is, to go through each row of table and check status1 (gv-field-15-139), status2 (gv-field-15-140) and status3 (gv-field-15-141). If one of this status (gv-field-15-150) is 'Completed' I want to print Completed in Status column. is it possible?






 $(document).ready(function(){
if(($('td#gv-field-15-139').text() == 'Completed') || ($('td#gv-field-15-
140').text() == 'Completed') || ($('td#gv-field-15-141').text() == 'Completed') ) {
$("td#gv-field-15-150").text('Completed');
}

    <table>
<tr>
<th>Student Name</th>
<th>Nationality</th>
<th>Status</th>
<th>Status1</th>
<th>Status2</th>
<th>Status3</th>
</tr>
<tr>
<td id="gv-field-15-1">Student A</td>
<td id="gv-field-15-90">India</td>
<td id="gv-field-15-150">&nbsp;</td>
<td id="gv-field-15-139">Completed</td>
<td id="gv-field-15-140">Pending</td>
<td id="gv-field-15-141">Pending</td>
</tr>
<tr>
<td id="gv-field-15-1">Student B</td>
<td id="gv-field-15-90">China</td>
<td id="gv-field-15-150">&nbsp;</td>
<td id="gv-field-15-139">Completed</td>
<td id="gv-field-15-140">Pending</td>
<td id="gv-field-15-141">Pending</td>
</tr>
</table>





This only works for 1 row and when it becomes 2 row or more it doesn't work. Could you please show me a way to achieve this.










share|improve this question
















I have a simple table, and I need to print a text in a certain column based on condition over hidden columns. There are multiple rows as a record which have same TD ID. What I need to do is, to go through each row of table and check status1 (gv-field-15-139), status2 (gv-field-15-140) and status3 (gv-field-15-141). If one of this status (gv-field-15-150) is 'Completed' I want to print Completed in Status column. is it possible?






 $(document).ready(function(){
if(($('td#gv-field-15-139').text() == 'Completed') || ($('td#gv-field-15-
140').text() == 'Completed') || ($('td#gv-field-15-141').text() == 'Completed') ) {
$("td#gv-field-15-150").text('Completed');
}

    <table>
<tr>
<th>Student Name</th>
<th>Nationality</th>
<th>Status</th>
<th>Status1</th>
<th>Status2</th>
<th>Status3</th>
</tr>
<tr>
<td id="gv-field-15-1">Student A</td>
<td id="gv-field-15-90">India</td>
<td id="gv-field-15-150">&nbsp;</td>
<td id="gv-field-15-139">Completed</td>
<td id="gv-field-15-140">Pending</td>
<td id="gv-field-15-141">Pending</td>
</tr>
<tr>
<td id="gv-field-15-1">Student B</td>
<td id="gv-field-15-90">China</td>
<td id="gv-field-15-150">&nbsp;</td>
<td id="gv-field-15-139">Completed</td>
<td id="gv-field-15-140">Pending</td>
<td id="gv-field-15-141">Pending</td>
</tr>
</table>





This only works for 1 row and when it becomes 2 row or more it doesn't work. Could you please show me a way to achieve this.






 $(document).ready(function(){
if(($('td#gv-field-15-139').text() == 'Completed') || ($('td#gv-field-15-
140').text() == 'Completed') || ($('td#gv-field-15-141').text() == 'Completed') ) {
$("td#gv-field-15-150").text('Completed');
}

    <table>
<tr>
<th>Student Name</th>
<th>Nationality</th>
<th>Status</th>
<th>Status1</th>
<th>Status2</th>
<th>Status3</th>
</tr>
<tr>
<td id="gv-field-15-1">Student A</td>
<td id="gv-field-15-90">India</td>
<td id="gv-field-15-150">&nbsp;</td>
<td id="gv-field-15-139">Completed</td>
<td id="gv-field-15-140">Pending</td>
<td id="gv-field-15-141">Pending</td>
</tr>
<tr>
<td id="gv-field-15-1">Student B</td>
<td id="gv-field-15-90">China</td>
<td id="gv-field-15-150">&nbsp;</td>
<td id="gv-field-15-139">Completed</td>
<td id="gv-field-15-140">Pending</td>
<td id="gv-field-15-141">Pending</td>
</tr>
</table>





 $(document).ready(function(){
if(($('td#gv-field-15-139').text() == 'Completed') || ($('td#gv-field-15-
140').text() == 'Completed') || ($('td#gv-field-15-141').text() == 'Completed') ) {
$("td#gv-field-15-150").text('Completed');
}

    <table>
<tr>
<th>Student Name</th>
<th>Nationality</th>
<th>Status</th>
<th>Status1</th>
<th>Status2</th>
<th>Status3</th>
</tr>
<tr>
<td id="gv-field-15-1">Student A</td>
<td id="gv-field-15-90">India</td>
<td id="gv-field-15-150">&nbsp;</td>
<td id="gv-field-15-139">Completed</td>
<td id="gv-field-15-140">Pending</td>
<td id="gv-field-15-141">Pending</td>
</tr>
<tr>
<td id="gv-field-15-1">Student B</td>
<td id="gv-field-15-90">China</td>
<td id="gv-field-15-150">&nbsp;</td>
<td id="gv-field-15-139">Completed</td>
<td id="gv-field-15-140">Pending</td>
<td id="gv-field-15-141">Pending</td>
</tr>
</table>






javascript html-table






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 16:50









Brian Tompsett - 汤莱恩

4,2571339103




4,2571339103










asked Jan 3 at 9:21









Amir NikfarAmir Nikfar

46110




46110













  • Do not duplicate ids, that's why you can't access past the first one. When JS finds an id, it naturally assumes that's the only tag with that id. Change your ids into classes.

    – zer00ne
    Jan 3 at 9:31





















  • Do not duplicate ids, that's why you can't access past the first one. When JS finds an id, it naturally assumes that's the only tag with that id. Change your ids into classes.

    – zer00ne
    Jan 3 at 9:31



















Do not duplicate ids, that's why you can't access past the first one. When JS finds an id, it naturally assumes that's the only tag with that id. Change your ids into classes.

– zer00ne
Jan 3 at 9:31







Do not duplicate ids, that's why you can't access past the first one. When JS finds an id, it naturally assumes that's the only tag with that id. Change your ids into classes.

– zer00ne
Jan 3 at 9:31














6 Answers
6






active

oldest

votes


















3














Loop your tr except the first tr which has th and check td. Though you can achieve the solution to your problem with ids, its recommended to use class instead of ids.






$(document).ready(function() {
var trs = $('tr').not(':eq(0)');
$.each(trs, function() {
var $this = $(this);
if (($this.find('>td#gv-field-15-139').text() == 'Completed') || ($this.find('>td#gv-field-15-140').text() == 'Completed') || ($this.find('>td#gv-field-15-141').text() == 'Completed')) {
$this.find(">td#gv-field-15-150").text('Completed');
}
});
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<table>
<tr>
<th>Student Name</th>
<th>Nationality</th>
<th>Status</th>
<th>Status1</th>
<th>Status2</th>
<th>Status3</th>
</tr>
<tr>
<td id="gv-field-15-1">Student A</td>
<td id="gv-field-15-90">India</td>
<td id="gv-field-15-150">&nbsp;</td>
<td id="gv-field-15-139">Completed</td>
<td id="gv-field-15-140">Pending</td>
<td id="gv-field-15-141">Pending</td>
</tr>
<tr>
<td id="gv-field-15-1">Student B</td>
<td id="gv-field-15-90">China</td>
<td id="gv-field-15-150">&nbsp;</td>
<td id="gv-field-15-139">Completed</td>
<td id="gv-field-15-140">Pending</td>
<td id="gv-field-15-141">Pending</td>
</tr>
<tr>
<td id="gv-field-15-1">Student B</td>
<td id="gv-field-15-90">China</td>
<td id="gv-field-15-150">&nbsp;</td>
<td id="gv-field-15-139">Pending</td>
<td id="gv-field-15-140">Pending</td>
<td id="gv-field-15-141">Pending</td>
</tr>
</table>








share|improve this answer































    2














    Do not use duplicate id's, you can use the index of the cells intead:






    const table = document.querySelector('table');
    //take all rows except the header
    const rows = Array.from(table.querySelectorAll('tr')).slice(
    1,
    );
    rows.forEach((row) => {
    const cells = Array.from(
    row.querySelectorAll('td')
    );
    //get the 3rd cell
    const [, , toSet] = cells;
    //all values after 3rd cell
    const [, , ,...values] = cells.map(
    (cell) => cell.innerText.trim()//map to the innerText of the cell
    );
    if(values.includes('Completed')){
    toSet.innerHTML = 'Completed'
    }
    });

    <table>
    <tr>
    <th>Student Name</th>
    <th>Nationality</th>
    <th>Status</th>
    <th>Status1</th>
    <th>Status2</th>
    <th>Status3</th>
    </tr>
    <tr>
    <td>Student A</td>
    <td>India</td>
    <td>xxx</td>
    <td>Completed</td>
    <td>Pending</td>
    <td>Pending</td>
    </tr>
    <tr>
    <td>Student B</td>
    <td>China</td>
    <td>xxx</td>
    <td>Completed</td>
    <td>Pending</td>
    <td>Pending</td>
    </tr>
    <tr>
    <td>Student C</td>
    <td>Not Completed</td>
    <td>xxx</td>
    <td>Pending</td>
    <td>Pending</td>
    <td>Pending</td>
    </tr>
    </table>








    share|improve this answer


























    • Probably the best answer so far, very nice solution!

      – JO3-W3B-D3V
      Jan 3 at 9:54



















    2














    This solution is so simplistic it doesn't even have a need for jQuery, I also thought that it might be nicer to read if you broke the logic up a bit, i.e. rather than having an ugly & long looking if statement, I've implemented the isComplete function.



    FYI. ID's are meant to be unique throughout the entire page, it may be a better idea to change them to classes if you can.



    Edit



    After having looked at the beautiful solution produced by @HMR I thought I'd include some of the elegant code that was implemented into this solution, I can't take all the credit there!






    // Just short hand.
    const $e = queryString => document.querySelectorAll(queryString);
    const term = 'Completed';

    // Returns a boolean.
    const isComplete = td => td.textContent.replace(/ /, '') === term;

    // Some fallback function.
    const fallback = () => console.log('Next...');

    // Render the update(s).
    const render = (v, t) => v.includes(true) ? t.textContent = term : fallback();

    // Updates the relevant td tags.
    const update = tr => {
    const tds = Array.from(tr.querySelectorAll('td'));
    const [, , , ...values] = tds.map(td => isComplete(td));
    const [, , td] = tds;
    render(values, td);
    };

    // Iterate over the relevant tr tags.
    const loop = () => Array.from($e("table tr")).splice(1, ).forEach(tr => update(tr));

    // Start the process.
    loop();

    <table>
    <tr>
    <th>Student Name</th>
    <th>Nationality</th>
    <th>Status</th>
    <th>Status1</th>
    <th>Status2</th>
    <th>Status3</th>
    </tr>
    <tr>
    <td id="gv-field-15-1">Student A</td>
    <td id="gv-field-15-90">India</td>
    <td id="gv-field-15-150">&nbsp;</td>
    <td id="gv-field-15-139">Completed</td>
    <td id="gv-field-15-140">Pending</td>
    <td id="gv-field-15-141">Pending</td>
    </tr>
    <tr>
    <td id="gv-field-15-1">Student B</td>
    <td id="gv-field-15-90">China</td>
    <td id="gv-field-15-150">&nbsp;</td>
    <td id="gv-field-15-139">Completed</td>
    <td id="gv-field-15-140">Pending</td>
    <td id="gv-field-15-141">Pending</td>
    </tr>
    <tr>
    <td id="gv-field-15-1">Student C</td>
    <td id="gv-field-15-90">China</td>
    <td id="gv-field-15-150">&nbsp;</td>
    <td id="gv-field-15-139">Pending</td>
    <td id="gv-field-15-140">Pending</td>
    <td id="gv-field-15-141">Pending</td>
    </tr>
    </table>








    share|improve this answer

































      0














      There are couple of issues:




      • You have duplicate IDs. ID must be unique. In your current code, you will always get first element with necessary ID. So you should use classes instead

      • You are not looping. Since its a table and you have to handle each row, you have to loop over all rows and based on current row, you have to check/update necessary column's value.





      $(document).ready(function() {

      var $tr = $('table tr');
      $.each($tr, function(index, tr) {
      if (
      ($('.gv-field-15-139', tr).text() == 'Completed') ||
      ($('.gv-field-15- 140', tr).text() == 'Completed') ||
      ($('.gv-field-15-141', tr).text() == 'Completed')
      ) {
      $(".gv-field-15-150", tr).text('Completed');
      }
      })
      });

      <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
      <table>
      <tr>
      <th>Student Name</th>
      <th>Nationality</th>
      <th>Status</th>
      <th>Status1</th>
      <th>Status2</th>
      <th>Status3</th>
      </tr>
      <tr>
      <td class="gv-field-15-1">Student A</td>
      <td class="gv-field-15-90">India</td>
      <td class="gv-field-15-150">&nbsp;</td>
      <td class="gv-field-15-139">Completed</td>
      <td class="gv-field-15-140">Pending</td>
      <td class="gv-field-15-141">Pending</td>
      </tr>
      <tr>
      <td class="gv-field-15-1">Student B</td>
      <td class="gv-field-15-90">China</td>
      <td class="gv-field-15-150">&nbsp;</td>
      <td class="gv-field-15-139">Pending</td>
      <td class="gv-field-15-140">Pending</td>
      <td class="gv-field-15-141">Pending</td>
      </tr>
      </table>








      share|improve this answer































        0














        Loop through all the rows in the table except the first row(and hence => $('tr').length-1).



        .eq() function is used to access each element/text by index..



            $(document).ready(function () {
        for(i=0;i<$('tr').length-1;i++){
        if( ($('td#gv-field-15-139').eq(i).text() == 'Completed') || ($('td#gv-field-15-140').eq(i).text() == 'Completed') || ($('td#gv - field - 15 - 141').eq(i).text() == 'Completed') ) {
        $("td#gv-field-15-150").eq(i).text('Completed');
        }
        }
        });





        share|improve this answer

































          0















          #IDs Must Always be Unique



          Having duplicated #ids makes HTML invalid, more importantly it cripples JS/jQ because it is expected that #ids are unique so once found, no efforts are made to continue for second occurrence of an #id.




          Changes




          • All #ids have been changed to .classes

          • The last 3 <td> of each row have a shared class: .status

          • The 3rd <td> of each row has a new class: .primary


          Reference



          .each()



          .prevAll()




          Demo



          Three lines of code...that's all you need.




          $('.status').each(function() {
          if ($(this).text() === 'Completed') {
          $(this).prevAll('.primary').text('Completed');
          }
          });

          <table>
          <tr>
          <th>Student Name</th>
          <th>Nationality</th>
          <th>Status</th>
          <th>Status1</th>
          <th>Status2</th>
          <th>Status3</th>
          </tr>
          <tr>
          <td class="gv-field-15-1">Student A</td>
          <td class="gv-field-15-90">India</td>
          <td class="gv-field-15-150 primary">&nbsp;</td>
          <td class="gv-field-15-139 status">Completed</td>
          <td class="gv-field-15-140 status">Pending</td>
          <td class="gv-field-15-141 status">Pending</td>
          </tr>
          <tr>
          <td class="gv-field-15-1">Student B</td>
          <td class="gv-field-15-90">China</td>
          <td class="gv-field-15-150 primary">&nbsp;</td>
          <td class="gv-field-15-139 status">Completed</td>
          <td class="gv-field-15-140 status">Pending</td>
          <td class="gv-field-15-141 status">Pending</td>
          </tr>
          <tr>
          <td class="gv-field-15-1">Student C</td>
          <td class="gv-field-15-90">USA</td>
          <td class="gv-field-15-150 primary">&nbsp;</td>
          <td class="gv-field-15-139 status">ERROR</td>
          <td class="gv-field-15-140 status">Pending</td>
          <td class="gv-field-15-141 status">Pending</td>
          </tr>
          <tr>
          <td class="gv-field-15-1">Student D</td>
          <td class="gv-field-15-90">France</td>
          <td class="gv-field-15-150 primary">&nbsp;</td>
          <td class="gv-field-15-139 status">Pending</td>
          <td class="gv-field-15-140 status">Pending</td>
          <td class="gv-field-15-141 status">Completed</td>
          </tr>
          </table>

          <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>








          share|improve this answer


























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54019365%2fjs-loop-through-table-and-print-text-in-column-based-on-condition%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            6 Answers
            6






            active

            oldest

            votes








            6 Answers
            6






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            3














            Loop your tr except the first tr which has th and check td. Though you can achieve the solution to your problem with ids, its recommended to use class instead of ids.






            $(document).ready(function() {
            var trs = $('tr').not(':eq(0)');
            $.each(trs, function() {
            var $this = $(this);
            if (($this.find('>td#gv-field-15-139').text() == 'Completed') || ($this.find('>td#gv-field-15-140').text() == 'Completed') || ($this.find('>td#gv-field-15-141').text() == 'Completed')) {
            $this.find(">td#gv-field-15-150").text('Completed');
            }
            });
            });

            <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

            <table>
            <tr>
            <th>Student Name</th>
            <th>Nationality</th>
            <th>Status</th>
            <th>Status1</th>
            <th>Status2</th>
            <th>Status3</th>
            </tr>
            <tr>
            <td id="gv-field-15-1">Student A</td>
            <td id="gv-field-15-90">India</td>
            <td id="gv-field-15-150">&nbsp;</td>
            <td id="gv-field-15-139">Completed</td>
            <td id="gv-field-15-140">Pending</td>
            <td id="gv-field-15-141">Pending</td>
            </tr>
            <tr>
            <td id="gv-field-15-1">Student B</td>
            <td id="gv-field-15-90">China</td>
            <td id="gv-field-15-150">&nbsp;</td>
            <td id="gv-field-15-139">Completed</td>
            <td id="gv-field-15-140">Pending</td>
            <td id="gv-field-15-141">Pending</td>
            </tr>
            <tr>
            <td id="gv-field-15-1">Student B</td>
            <td id="gv-field-15-90">China</td>
            <td id="gv-field-15-150">&nbsp;</td>
            <td id="gv-field-15-139">Pending</td>
            <td id="gv-field-15-140">Pending</td>
            <td id="gv-field-15-141">Pending</td>
            </tr>
            </table>








            share|improve this answer




























              3














              Loop your tr except the first tr which has th and check td. Though you can achieve the solution to your problem with ids, its recommended to use class instead of ids.






              $(document).ready(function() {
              var trs = $('tr').not(':eq(0)');
              $.each(trs, function() {
              var $this = $(this);
              if (($this.find('>td#gv-field-15-139').text() == 'Completed') || ($this.find('>td#gv-field-15-140').text() == 'Completed') || ($this.find('>td#gv-field-15-141').text() == 'Completed')) {
              $this.find(">td#gv-field-15-150").text('Completed');
              }
              });
              });

              <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

              <table>
              <tr>
              <th>Student Name</th>
              <th>Nationality</th>
              <th>Status</th>
              <th>Status1</th>
              <th>Status2</th>
              <th>Status3</th>
              </tr>
              <tr>
              <td id="gv-field-15-1">Student A</td>
              <td id="gv-field-15-90">India</td>
              <td id="gv-field-15-150">&nbsp;</td>
              <td id="gv-field-15-139">Completed</td>
              <td id="gv-field-15-140">Pending</td>
              <td id="gv-field-15-141">Pending</td>
              </tr>
              <tr>
              <td id="gv-field-15-1">Student B</td>
              <td id="gv-field-15-90">China</td>
              <td id="gv-field-15-150">&nbsp;</td>
              <td id="gv-field-15-139">Completed</td>
              <td id="gv-field-15-140">Pending</td>
              <td id="gv-field-15-141">Pending</td>
              </tr>
              <tr>
              <td id="gv-field-15-1">Student B</td>
              <td id="gv-field-15-90">China</td>
              <td id="gv-field-15-150">&nbsp;</td>
              <td id="gv-field-15-139">Pending</td>
              <td id="gv-field-15-140">Pending</td>
              <td id="gv-field-15-141">Pending</td>
              </tr>
              </table>








              share|improve this answer


























                3












                3








                3







                Loop your tr except the first tr which has th and check td. Though you can achieve the solution to your problem with ids, its recommended to use class instead of ids.






                $(document).ready(function() {
                var trs = $('tr').not(':eq(0)');
                $.each(trs, function() {
                var $this = $(this);
                if (($this.find('>td#gv-field-15-139').text() == 'Completed') || ($this.find('>td#gv-field-15-140').text() == 'Completed') || ($this.find('>td#gv-field-15-141').text() == 'Completed')) {
                $this.find(">td#gv-field-15-150").text('Completed');
                }
                });
                });

                <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

                <table>
                <tr>
                <th>Student Name</th>
                <th>Nationality</th>
                <th>Status</th>
                <th>Status1</th>
                <th>Status2</th>
                <th>Status3</th>
                </tr>
                <tr>
                <td id="gv-field-15-1">Student A</td>
                <td id="gv-field-15-90">India</td>
                <td id="gv-field-15-150">&nbsp;</td>
                <td id="gv-field-15-139">Completed</td>
                <td id="gv-field-15-140">Pending</td>
                <td id="gv-field-15-141">Pending</td>
                </tr>
                <tr>
                <td id="gv-field-15-1">Student B</td>
                <td id="gv-field-15-90">China</td>
                <td id="gv-field-15-150">&nbsp;</td>
                <td id="gv-field-15-139">Completed</td>
                <td id="gv-field-15-140">Pending</td>
                <td id="gv-field-15-141">Pending</td>
                </tr>
                <tr>
                <td id="gv-field-15-1">Student B</td>
                <td id="gv-field-15-90">China</td>
                <td id="gv-field-15-150">&nbsp;</td>
                <td id="gv-field-15-139">Pending</td>
                <td id="gv-field-15-140">Pending</td>
                <td id="gv-field-15-141">Pending</td>
                </tr>
                </table>








                share|improve this answer













                Loop your tr except the first tr which has th and check td. Though you can achieve the solution to your problem with ids, its recommended to use class instead of ids.






                $(document).ready(function() {
                var trs = $('tr').not(':eq(0)');
                $.each(trs, function() {
                var $this = $(this);
                if (($this.find('>td#gv-field-15-139').text() == 'Completed') || ($this.find('>td#gv-field-15-140').text() == 'Completed') || ($this.find('>td#gv-field-15-141').text() == 'Completed')) {
                $this.find(">td#gv-field-15-150").text('Completed');
                }
                });
                });

                <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

                <table>
                <tr>
                <th>Student Name</th>
                <th>Nationality</th>
                <th>Status</th>
                <th>Status1</th>
                <th>Status2</th>
                <th>Status3</th>
                </tr>
                <tr>
                <td id="gv-field-15-1">Student A</td>
                <td id="gv-field-15-90">India</td>
                <td id="gv-field-15-150">&nbsp;</td>
                <td id="gv-field-15-139">Completed</td>
                <td id="gv-field-15-140">Pending</td>
                <td id="gv-field-15-141">Pending</td>
                </tr>
                <tr>
                <td id="gv-field-15-1">Student B</td>
                <td id="gv-field-15-90">China</td>
                <td id="gv-field-15-150">&nbsp;</td>
                <td id="gv-field-15-139">Completed</td>
                <td id="gv-field-15-140">Pending</td>
                <td id="gv-field-15-141">Pending</td>
                </tr>
                <tr>
                <td id="gv-field-15-1">Student B</td>
                <td id="gv-field-15-90">China</td>
                <td id="gv-field-15-150">&nbsp;</td>
                <td id="gv-field-15-139">Pending</td>
                <td id="gv-field-15-140">Pending</td>
                <td id="gv-field-15-141">Pending</td>
                </tr>
                </table>








                $(document).ready(function() {
                var trs = $('tr').not(':eq(0)');
                $.each(trs, function() {
                var $this = $(this);
                if (($this.find('>td#gv-field-15-139').text() == 'Completed') || ($this.find('>td#gv-field-15-140').text() == 'Completed') || ($this.find('>td#gv-field-15-141').text() == 'Completed')) {
                $this.find(">td#gv-field-15-150").text('Completed');
                }
                });
                });

                <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

                <table>
                <tr>
                <th>Student Name</th>
                <th>Nationality</th>
                <th>Status</th>
                <th>Status1</th>
                <th>Status2</th>
                <th>Status3</th>
                </tr>
                <tr>
                <td id="gv-field-15-1">Student A</td>
                <td id="gv-field-15-90">India</td>
                <td id="gv-field-15-150">&nbsp;</td>
                <td id="gv-field-15-139">Completed</td>
                <td id="gv-field-15-140">Pending</td>
                <td id="gv-field-15-141">Pending</td>
                </tr>
                <tr>
                <td id="gv-field-15-1">Student B</td>
                <td id="gv-field-15-90">China</td>
                <td id="gv-field-15-150">&nbsp;</td>
                <td id="gv-field-15-139">Completed</td>
                <td id="gv-field-15-140">Pending</td>
                <td id="gv-field-15-141">Pending</td>
                </tr>
                <tr>
                <td id="gv-field-15-1">Student B</td>
                <td id="gv-field-15-90">China</td>
                <td id="gv-field-15-150">&nbsp;</td>
                <td id="gv-field-15-139">Pending</td>
                <td id="gv-field-15-140">Pending</td>
                <td id="gv-field-15-141">Pending</td>
                </tr>
                </table>





                $(document).ready(function() {
                var trs = $('tr').not(':eq(0)');
                $.each(trs, function() {
                var $this = $(this);
                if (($this.find('>td#gv-field-15-139').text() == 'Completed') || ($this.find('>td#gv-field-15-140').text() == 'Completed') || ($this.find('>td#gv-field-15-141').text() == 'Completed')) {
                $this.find(">td#gv-field-15-150").text('Completed');
                }
                });
                });

                <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

                <table>
                <tr>
                <th>Student Name</th>
                <th>Nationality</th>
                <th>Status</th>
                <th>Status1</th>
                <th>Status2</th>
                <th>Status3</th>
                </tr>
                <tr>
                <td id="gv-field-15-1">Student A</td>
                <td id="gv-field-15-90">India</td>
                <td id="gv-field-15-150">&nbsp;</td>
                <td id="gv-field-15-139">Completed</td>
                <td id="gv-field-15-140">Pending</td>
                <td id="gv-field-15-141">Pending</td>
                </tr>
                <tr>
                <td id="gv-field-15-1">Student B</td>
                <td id="gv-field-15-90">China</td>
                <td id="gv-field-15-150">&nbsp;</td>
                <td id="gv-field-15-139">Completed</td>
                <td id="gv-field-15-140">Pending</td>
                <td id="gv-field-15-141">Pending</td>
                </tr>
                <tr>
                <td id="gv-field-15-1">Student B</td>
                <td id="gv-field-15-90">China</td>
                <td id="gv-field-15-150">&nbsp;</td>
                <td id="gv-field-15-139">Pending</td>
                <td id="gv-field-15-140">Pending</td>
                <td id="gv-field-15-141">Pending</td>
                </tr>
                </table>






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 3 at 9:40









                Kiran ShahiKiran Shahi

                5,12441846




                5,12441846

























                    2














                    Do not use duplicate id's, you can use the index of the cells intead:






                    const table = document.querySelector('table');
                    //take all rows except the header
                    const rows = Array.from(table.querySelectorAll('tr')).slice(
                    1,
                    );
                    rows.forEach((row) => {
                    const cells = Array.from(
                    row.querySelectorAll('td')
                    );
                    //get the 3rd cell
                    const [, , toSet] = cells;
                    //all values after 3rd cell
                    const [, , ,...values] = cells.map(
                    (cell) => cell.innerText.trim()//map to the innerText of the cell
                    );
                    if(values.includes('Completed')){
                    toSet.innerHTML = 'Completed'
                    }
                    });

                    <table>
                    <tr>
                    <th>Student Name</th>
                    <th>Nationality</th>
                    <th>Status</th>
                    <th>Status1</th>
                    <th>Status2</th>
                    <th>Status3</th>
                    </tr>
                    <tr>
                    <td>Student A</td>
                    <td>India</td>
                    <td>xxx</td>
                    <td>Completed</td>
                    <td>Pending</td>
                    <td>Pending</td>
                    </tr>
                    <tr>
                    <td>Student B</td>
                    <td>China</td>
                    <td>xxx</td>
                    <td>Completed</td>
                    <td>Pending</td>
                    <td>Pending</td>
                    </tr>
                    <tr>
                    <td>Student C</td>
                    <td>Not Completed</td>
                    <td>xxx</td>
                    <td>Pending</td>
                    <td>Pending</td>
                    <td>Pending</td>
                    </tr>
                    </table>








                    share|improve this answer


























                    • Probably the best answer so far, very nice solution!

                      – JO3-W3B-D3V
                      Jan 3 at 9:54
















                    2














                    Do not use duplicate id's, you can use the index of the cells intead:






                    const table = document.querySelector('table');
                    //take all rows except the header
                    const rows = Array.from(table.querySelectorAll('tr')).slice(
                    1,
                    );
                    rows.forEach((row) => {
                    const cells = Array.from(
                    row.querySelectorAll('td')
                    );
                    //get the 3rd cell
                    const [, , toSet] = cells;
                    //all values after 3rd cell
                    const [, , ,...values] = cells.map(
                    (cell) => cell.innerText.trim()//map to the innerText of the cell
                    );
                    if(values.includes('Completed')){
                    toSet.innerHTML = 'Completed'
                    }
                    });

                    <table>
                    <tr>
                    <th>Student Name</th>
                    <th>Nationality</th>
                    <th>Status</th>
                    <th>Status1</th>
                    <th>Status2</th>
                    <th>Status3</th>
                    </tr>
                    <tr>
                    <td>Student A</td>
                    <td>India</td>
                    <td>xxx</td>
                    <td>Completed</td>
                    <td>Pending</td>
                    <td>Pending</td>
                    </tr>
                    <tr>
                    <td>Student B</td>
                    <td>China</td>
                    <td>xxx</td>
                    <td>Completed</td>
                    <td>Pending</td>
                    <td>Pending</td>
                    </tr>
                    <tr>
                    <td>Student C</td>
                    <td>Not Completed</td>
                    <td>xxx</td>
                    <td>Pending</td>
                    <td>Pending</td>
                    <td>Pending</td>
                    </tr>
                    </table>








                    share|improve this answer


























                    • Probably the best answer so far, very nice solution!

                      – JO3-W3B-D3V
                      Jan 3 at 9:54














                    2












                    2








                    2







                    Do not use duplicate id's, you can use the index of the cells intead:






                    const table = document.querySelector('table');
                    //take all rows except the header
                    const rows = Array.from(table.querySelectorAll('tr')).slice(
                    1,
                    );
                    rows.forEach((row) => {
                    const cells = Array.from(
                    row.querySelectorAll('td')
                    );
                    //get the 3rd cell
                    const [, , toSet] = cells;
                    //all values after 3rd cell
                    const [, , ,...values] = cells.map(
                    (cell) => cell.innerText.trim()//map to the innerText of the cell
                    );
                    if(values.includes('Completed')){
                    toSet.innerHTML = 'Completed'
                    }
                    });

                    <table>
                    <tr>
                    <th>Student Name</th>
                    <th>Nationality</th>
                    <th>Status</th>
                    <th>Status1</th>
                    <th>Status2</th>
                    <th>Status3</th>
                    </tr>
                    <tr>
                    <td>Student A</td>
                    <td>India</td>
                    <td>xxx</td>
                    <td>Completed</td>
                    <td>Pending</td>
                    <td>Pending</td>
                    </tr>
                    <tr>
                    <td>Student B</td>
                    <td>China</td>
                    <td>xxx</td>
                    <td>Completed</td>
                    <td>Pending</td>
                    <td>Pending</td>
                    </tr>
                    <tr>
                    <td>Student C</td>
                    <td>Not Completed</td>
                    <td>xxx</td>
                    <td>Pending</td>
                    <td>Pending</td>
                    <td>Pending</td>
                    </tr>
                    </table>








                    share|improve this answer















                    Do not use duplicate id's, you can use the index of the cells intead:






                    const table = document.querySelector('table');
                    //take all rows except the header
                    const rows = Array.from(table.querySelectorAll('tr')).slice(
                    1,
                    );
                    rows.forEach((row) => {
                    const cells = Array.from(
                    row.querySelectorAll('td')
                    );
                    //get the 3rd cell
                    const [, , toSet] = cells;
                    //all values after 3rd cell
                    const [, , ,...values] = cells.map(
                    (cell) => cell.innerText.trim()//map to the innerText of the cell
                    );
                    if(values.includes('Completed')){
                    toSet.innerHTML = 'Completed'
                    }
                    });

                    <table>
                    <tr>
                    <th>Student Name</th>
                    <th>Nationality</th>
                    <th>Status</th>
                    <th>Status1</th>
                    <th>Status2</th>
                    <th>Status3</th>
                    </tr>
                    <tr>
                    <td>Student A</td>
                    <td>India</td>
                    <td>xxx</td>
                    <td>Completed</td>
                    <td>Pending</td>
                    <td>Pending</td>
                    </tr>
                    <tr>
                    <td>Student B</td>
                    <td>China</td>
                    <td>xxx</td>
                    <td>Completed</td>
                    <td>Pending</td>
                    <td>Pending</td>
                    </tr>
                    <tr>
                    <td>Student C</td>
                    <td>Not Completed</td>
                    <td>xxx</td>
                    <td>Pending</td>
                    <td>Pending</td>
                    <td>Pending</td>
                    </tr>
                    </table>








                    const table = document.querySelector('table');
                    //take all rows except the header
                    const rows = Array.from(table.querySelectorAll('tr')).slice(
                    1,
                    );
                    rows.forEach((row) => {
                    const cells = Array.from(
                    row.querySelectorAll('td')
                    );
                    //get the 3rd cell
                    const [, , toSet] = cells;
                    //all values after 3rd cell
                    const [, , ,...values] = cells.map(
                    (cell) => cell.innerText.trim()//map to the innerText of the cell
                    );
                    if(values.includes('Completed')){
                    toSet.innerHTML = 'Completed'
                    }
                    });

                    <table>
                    <tr>
                    <th>Student Name</th>
                    <th>Nationality</th>
                    <th>Status</th>
                    <th>Status1</th>
                    <th>Status2</th>
                    <th>Status3</th>
                    </tr>
                    <tr>
                    <td>Student A</td>
                    <td>India</td>
                    <td>xxx</td>
                    <td>Completed</td>
                    <td>Pending</td>
                    <td>Pending</td>
                    </tr>
                    <tr>
                    <td>Student B</td>
                    <td>China</td>
                    <td>xxx</td>
                    <td>Completed</td>
                    <td>Pending</td>
                    <td>Pending</td>
                    </tr>
                    <tr>
                    <td>Student C</td>
                    <td>Not Completed</td>
                    <td>xxx</td>
                    <td>Pending</td>
                    <td>Pending</td>
                    <td>Pending</td>
                    </tr>
                    </table>





                    const table = document.querySelector('table');
                    //take all rows except the header
                    const rows = Array.from(table.querySelectorAll('tr')).slice(
                    1,
                    );
                    rows.forEach((row) => {
                    const cells = Array.from(
                    row.querySelectorAll('td')
                    );
                    //get the 3rd cell
                    const [, , toSet] = cells;
                    //all values after 3rd cell
                    const [, , ,...values] = cells.map(
                    (cell) => cell.innerText.trim()//map to the innerText of the cell
                    );
                    if(values.includes('Completed')){
                    toSet.innerHTML = 'Completed'
                    }
                    });

                    <table>
                    <tr>
                    <th>Student Name</th>
                    <th>Nationality</th>
                    <th>Status</th>
                    <th>Status1</th>
                    <th>Status2</th>
                    <th>Status3</th>
                    </tr>
                    <tr>
                    <td>Student A</td>
                    <td>India</td>
                    <td>xxx</td>
                    <td>Completed</td>
                    <td>Pending</td>
                    <td>Pending</td>
                    </tr>
                    <tr>
                    <td>Student B</td>
                    <td>China</td>
                    <td>xxx</td>
                    <td>Completed</td>
                    <td>Pending</td>
                    <td>Pending</td>
                    </tr>
                    <tr>
                    <td>Student C</td>
                    <td>Not Completed</td>
                    <td>xxx</td>
                    <td>Pending</td>
                    <td>Pending</td>
                    <td>Pending</td>
                    </tr>
                    </table>






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Jan 3 at 9:53

























                    answered Jan 3 at 9:47









                    HMRHMR

                    14.1k1138102




                    14.1k1138102













                    • Probably the best answer so far, very nice solution!

                      – JO3-W3B-D3V
                      Jan 3 at 9:54



















                    • Probably the best answer so far, very nice solution!

                      – JO3-W3B-D3V
                      Jan 3 at 9:54

















                    Probably the best answer so far, very nice solution!

                    – JO3-W3B-D3V
                    Jan 3 at 9:54





                    Probably the best answer so far, very nice solution!

                    – JO3-W3B-D3V
                    Jan 3 at 9:54











                    2














                    This solution is so simplistic it doesn't even have a need for jQuery, I also thought that it might be nicer to read if you broke the logic up a bit, i.e. rather than having an ugly & long looking if statement, I've implemented the isComplete function.



                    FYI. ID's are meant to be unique throughout the entire page, it may be a better idea to change them to classes if you can.



                    Edit



                    After having looked at the beautiful solution produced by @HMR I thought I'd include some of the elegant code that was implemented into this solution, I can't take all the credit there!






                    // Just short hand.
                    const $e = queryString => document.querySelectorAll(queryString);
                    const term = 'Completed';

                    // Returns a boolean.
                    const isComplete = td => td.textContent.replace(/ /, '') === term;

                    // Some fallback function.
                    const fallback = () => console.log('Next...');

                    // Render the update(s).
                    const render = (v, t) => v.includes(true) ? t.textContent = term : fallback();

                    // Updates the relevant td tags.
                    const update = tr => {
                    const tds = Array.from(tr.querySelectorAll('td'));
                    const [, , , ...values] = tds.map(td => isComplete(td));
                    const [, , td] = tds;
                    render(values, td);
                    };

                    // Iterate over the relevant tr tags.
                    const loop = () => Array.from($e("table tr")).splice(1, ).forEach(tr => update(tr));

                    // Start the process.
                    loop();

                    <table>
                    <tr>
                    <th>Student Name</th>
                    <th>Nationality</th>
                    <th>Status</th>
                    <th>Status1</th>
                    <th>Status2</th>
                    <th>Status3</th>
                    </tr>
                    <tr>
                    <td id="gv-field-15-1">Student A</td>
                    <td id="gv-field-15-90">India</td>
                    <td id="gv-field-15-150">&nbsp;</td>
                    <td id="gv-field-15-139">Completed</td>
                    <td id="gv-field-15-140">Pending</td>
                    <td id="gv-field-15-141">Pending</td>
                    </tr>
                    <tr>
                    <td id="gv-field-15-1">Student B</td>
                    <td id="gv-field-15-90">China</td>
                    <td id="gv-field-15-150">&nbsp;</td>
                    <td id="gv-field-15-139">Completed</td>
                    <td id="gv-field-15-140">Pending</td>
                    <td id="gv-field-15-141">Pending</td>
                    </tr>
                    <tr>
                    <td id="gv-field-15-1">Student C</td>
                    <td id="gv-field-15-90">China</td>
                    <td id="gv-field-15-150">&nbsp;</td>
                    <td id="gv-field-15-139">Pending</td>
                    <td id="gv-field-15-140">Pending</td>
                    <td id="gv-field-15-141">Pending</td>
                    </tr>
                    </table>








                    share|improve this answer






























                      2














                      This solution is so simplistic it doesn't even have a need for jQuery, I also thought that it might be nicer to read if you broke the logic up a bit, i.e. rather than having an ugly & long looking if statement, I've implemented the isComplete function.



                      FYI. ID's are meant to be unique throughout the entire page, it may be a better idea to change them to classes if you can.



                      Edit



                      After having looked at the beautiful solution produced by @HMR I thought I'd include some of the elegant code that was implemented into this solution, I can't take all the credit there!






                      // Just short hand.
                      const $e = queryString => document.querySelectorAll(queryString);
                      const term = 'Completed';

                      // Returns a boolean.
                      const isComplete = td => td.textContent.replace(/ /, '') === term;

                      // Some fallback function.
                      const fallback = () => console.log('Next...');

                      // Render the update(s).
                      const render = (v, t) => v.includes(true) ? t.textContent = term : fallback();

                      // Updates the relevant td tags.
                      const update = tr => {
                      const tds = Array.from(tr.querySelectorAll('td'));
                      const [, , , ...values] = tds.map(td => isComplete(td));
                      const [, , td] = tds;
                      render(values, td);
                      };

                      // Iterate over the relevant tr tags.
                      const loop = () => Array.from($e("table tr")).splice(1, ).forEach(tr => update(tr));

                      // Start the process.
                      loop();

                      <table>
                      <tr>
                      <th>Student Name</th>
                      <th>Nationality</th>
                      <th>Status</th>
                      <th>Status1</th>
                      <th>Status2</th>
                      <th>Status3</th>
                      </tr>
                      <tr>
                      <td id="gv-field-15-1">Student A</td>
                      <td id="gv-field-15-90">India</td>
                      <td id="gv-field-15-150">&nbsp;</td>
                      <td id="gv-field-15-139">Completed</td>
                      <td id="gv-field-15-140">Pending</td>
                      <td id="gv-field-15-141">Pending</td>
                      </tr>
                      <tr>
                      <td id="gv-field-15-1">Student B</td>
                      <td id="gv-field-15-90">China</td>
                      <td id="gv-field-15-150">&nbsp;</td>
                      <td id="gv-field-15-139">Completed</td>
                      <td id="gv-field-15-140">Pending</td>
                      <td id="gv-field-15-141">Pending</td>
                      </tr>
                      <tr>
                      <td id="gv-field-15-1">Student C</td>
                      <td id="gv-field-15-90">China</td>
                      <td id="gv-field-15-150">&nbsp;</td>
                      <td id="gv-field-15-139">Pending</td>
                      <td id="gv-field-15-140">Pending</td>
                      <td id="gv-field-15-141">Pending</td>
                      </tr>
                      </table>








                      share|improve this answer




























                        2












                        2








                        2







                        This solution is so simplistic it doesn't even have a need for jQuery, I also thought that it might be nicer to read if you broke the logic up a bit, i.e. rather than having an ugly & long looking if statement, I've implemented the isComplete function.



                        FYI. ID's are meant to be unique throughout the entire page, it may be a better idea to change them to classes if you can.



                        Edit



                        After having looked at the beautiful solution produced by @HMR I thought I'd include some of the elegant code that was implemented into this solution, I can't take all the credit there!






                        // Just short hand.
                        const $e = queryString => document.querySelectorAll(queryString);
                        const term = 'Completed';

                        // Returns a boolean.
                        const isComplete = td => td.textContent.replace(/ /, '') === term;

                        // Some fallback function.
                        const fallback = () => console.log('Next...');

                        // Render the update(s).
                        const render = (v, t) => v.includes(true) ? t.textContent = term : fallback();

                        // Updates the relevant td tags.
                        const update = tr => {
                        const tds = Array.from(tr.querySelectorAll('td'));
                        const [, , , ...values] = tds.map(td => isComplete(td));
                        const [, , td] = tds;
                        render(values, td);
                        };

                        // Iterate over the relevant tr tags.
                        const loop = () => Array.from($e("table tr")).splice(1, ).forEach(tr => update(tr));

                        // Start the process.
                        loop();

                        <table>
                        <tr>
                        <th>Student Name</th>
                        <th>Nationality</th>
                        <th>Status</th>
                        <th>Status1</th>
                        <th>Status2</th>
                        <th>Status3</th>
                        </tr>
                        <tr>
                        <td id="gv-field-15-1">Student A</td>
                        <td id="gv-field-15-90">India</td>
                        <td id="gv-field-15-150">&nbsp;</td>
                        <td id="gv-field-15-139">Completed</td>
                        <td id="gv-field-15-140">Pending</td>
                        <td id="gv-field-15-141">Pending</td>
                        </tr>
                        <tr>
                        <td id="gv-field-15-1">Student B</td>
                        <td id="gv-field-15-90">China</td>
                        <td id="gv-field-15-150">&nbsp;</td>
                        <td id="gv-field-15-139">Completed</td>
                        <td id="gv-field-15-140">Pending</td>
                        <td id="gv-field-15-141">Pending</td>
                        </tr>
                        <tr>
                        <td id="gv-field-15-1">Student C</td>
                        <td id="gv-field-15-90">China</td>
                        <td id="gv-field-15-150">&nbsp;</td>
                        <td id="gv-field-15-139">Pending</td>
                        <td id="gv-field-15-140">Pending</td>
                        <td id="gv-field-15-141">Pending</td>
                        </tr>
                        </table>








                        share|improve this answer















                        This solution is so simplistic it doesn't even have a need for jQuery, I also thought that it might be nicer to read if you broke the logic up a bit, i.e. rather than having an ugly & long looking if statement, I've implemented the isComplete function.



                        FYI. ID's are meant to be unique throughout the entire page, it may be a better idea to change them to classes if you can.



                        Edit



                        After having looked at the beautiful solution produced by @HMR I thought I'd include some of the elegant code that was implemented into this solution, I can't take all the credit there!






                        // Just short hand.
                        const $e = queryString => document.querySelectorAll(queryString);
                        const term = 'Completed';

                        // Returns a boolean.
                        const isComplete = td => td.textContent.replace(/ /, '') === term;

                        // Some fallback function.
                        const fallback = () => console.log('Next...');

                        // Render the update(s).
                        const render = (v, t) => v.includes(true) ? t.textContent = term : fallback();

                        // Updates the relevant td tags.
                        const update = tr => {
                        const tds = Array.from(tr.querySelectorAll('td'));
                        const [, , , ...values] = tds.map(td => isComplete(td));
                        const [, , td] = tds;
                        render(values, td);
                        };

                        // Iterate over the relevant tr tags.
                        const loop = () => Array.from($e("table tr")).splice(1, ).forEach(tr => update(tr));

                        // Start the process.
                        loop();

                        <table>
                        <tr>
                        <th>Student Name</th>
                        <th>Nationality</th>
                        <th>Status</th>
                        <th>Status1</th>
                        <th>Status2</th>
                        <th>Status3</th>
                        </tr>
                        <tr>
                        <td id="gv-field-15-1">Student A</td>
                        <td id="gv-field-15-90">India</td>
                        <td id="gv-field-15-150">&nbsp;</td>
                        <td id="gv-field-15-139">Completed</td>
                        <td id="gv-field-15-140">Pending</td>
                        <td id="gv-field-15-141">Pending</td>
                        </tr>
                        <tr>
                        <td id="gv-field-15-1">Student B</td>
                        <td id="gv-field-15-90">China</td>
                        <td id="gv-field-15-150">&nbsp;</td>
                        <td id="gv-field-15-139">Completed</td>
                        <td id="gv-field-15-140">Pending</td>
                        <td id="gv-field-15-141">Pending</td>
                        </tr>
                        <tr>
                        <td id="gv-field-15-1">Student C</td>
                        <td id="gv-field-15-90">China</td>
                        <td id="gv-field-15-150">&nbsp;</td>
                        <td id="gv-field-15-139">Pending</td>
                        <td id="gv-field-15-140">Pending</td>
                        <td id="gv-field-15-141">Pending</td>
                        </tr>
                        </table>








                        // Just short hand.
                        const $e = queryString => document.querySelectorAll(queryString);
                        const term = 'Completed';

                        // Returns a boolean.
                        const isComplete = td => td.textContent.replace(/ /, '') === term;

                        // Some fallback function.
                        const fallback = () => console.log('Next...');

                        // Render the update(s).
                        const render = (v, t) => v.includes(true) ? t.textContent = term : fallback();

                        // Updates the relevant td tags.
                        const update = tr => {
                        const tds = Array.from(tr.querySelectorAll('td'));
                        const [, , , ...values] = tds.map(td => isComplete(td));
                        const [, , td] = tds;
                        render(values, td);
                        };

                        // Iterate over the relevant tr tags.
                        const loop = () => Array.from($e("table tr")).splice(1, ).forEach(tr => update(tr));

                        // Start the process.
                        loop();

                        <table>
                        <tr>
                        <th>Student Name</th>
                        <th>Nationality</th>
                        <th>Status</th>
                        <th>Status1</th>
                        <th>Status2</th>
                        <th>Status3</th>
                        </tr>
                        <tr>
                        <td id="gv-field-15-1">Student A</td>
                        <td id="gv-field-15-90">India</td>
                        <td id="gv-field-15-150">&nbsp;</td>
                        <td id="gv-field-15-139">Completed</td>
                        <td id="gv-field-15-140">Pending</td>
                        <td id="gv-field-15-141">Pending</td>
                        </tr>
                        <tr>
                        <td id="gv-field-15-1">Student B</td>
                        <td id="gv-field-15-90">China</td>
                        <td id="gv-field-15-150">&nbsp;</td>
                        <td id="gv-field-15-139">Completed</td>
                        <td id="gv-field-15-140">Pending</td>
                        <td id="gv-field-15-141">Pending</td>
                        </tr>
                        <tr>
                        <td id="gv-field-15-1">Student C</td>
                        <td id="gv-field-15-90">China</td>
                        <td id="gv-field-15-150">&nbsp;</td>
                        <td id="gv-field-15-139">Pending</td>
                        <td id="gv-field-15-140">Pending</td>
                        <td id="gv-field-15-141">Pending</td>
                        </tr>
                        </table>





                        // Just short hand.
                        const $e = queryString => document.querySelectorAll(queryString);
                        const term = 'Completed';

                        // Returns a boolean.
                        const isComplete = td => td.textContent.replace(/ /, '') === term;

                        // Some fallback function.
                        const fallback = () => console.log('Next...');

                        // Render the update(s).
                        const render = (v, t) => v.includes(true) ? t.textContent = term : fallback();

                        // Updates the relevant td tags.
                        const update = tr => {
                        const tds = Array.from(tr.querySelectorAll('td'));
                        const [, , , ...values] = tds.map(td => isComplete(td));
                        const [, , td] = tds;
                        render(values, td);
                        };

                        // Iterate over the relevant tr tags.
                        const loop = () => Array.from($e("table tr")).splice(1, ).forEach(tr => update(tr));

                        // Start the process.
                        loop();

                        <table>
                        <tr>
                        <th>Student Name</th>
                        <th>Nationality</th>
                        <th>Status</th>
                        <th>Status1</th>
                        <th>Status2</th>
                        <th>Status3</th>
                        </tr>
                        <tr>
                        <td id="gv-field-15-1">Student A</td>
                        <td id="gv-field-15-90">India</td>
                        <td id="gv-field-15-150">&nbsp;</td>
                        <td id="gv-field-15-139">Completed</td>
                        <td id="gv-field-15-140">Pending</td>
                        <td id="gv-field-15-141">Pending</td>
                        </tr>
                        <tr>
                        <td id="gv-field-15-1">Student B</td>
                        <td id="gv-field-15-90">China</td>
                        <td id="gv-field-15-150">&nbsp;</td>
                        <td id="gv-field-15-139">Completed</td>
                        <td id="gv-field-15-140">Pending</td>
                        <td id="gv-field-15-141">Pending</td>
                        </tr>
                        <tr>
                        <td id="gv-field-15-1">Student C</td>
                        <td id="gv-field-15-90">China</td>
                        <td id="gv-field-15-150">&nbsp;</td>
                        <td id="gv-field-15-139">Pending</td>
                        <td id="gv-field-15-140">Pending</td>
                        <td id="gv-field-15-141">Pending</td>
                        </tr>
                        </table>






                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Jan 3 at 10:09

























                        answered Jan 3 at 9:42









                        JO3-W3B-D3VJO3-W3B-D3V

                        1,513420




                        1,513420























                            0














                            There are couple of issues:




                            • You have duplicate IDs. ID must be unique. In your current code, you will always get first element with necessary ID. So you should use classes instead

                            • You are not looping. Since its a table and you have to handle each row, you have to loop over all rows and based on current row, you have to check/update necessary column's value.





                            $(document).ready(function() {

                            var $tr = $('table tr');
                            $.each($tr, function(index, tr) {
                            if (
                            ($('.gv-field-15-139', tr).text() == 'Completed') ||
                            ($('.gv-field-15- 140', tr).text() == 'Completed') ||
                            ($('.gv-field-15-141', tr).text() == 'Completed')
                            ) {
                            $(".gv-field-15-150", tr).text('Completed');
                            }
                            })
                            });

                            <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
                            <table>
                            <tr>
                            <th>Student Name</th>
                            <th>Nationality</th>
                            <th>Status</th>
                            <th>Status1</th>
                            <th>Status2</th>
                            <th>Status3</th>
                            </tr>
                            <tr>
                            <td class="gv-field-15-1">Student A</td>
                            <td class="gv-field-15-90">India</td>
                            <td class="gv-field-15-150">&nbsp;</td>
                            <td class="gv-field-15-139">Completed</td>
                            <td class="gv-field-15-140">Pending</td>
                            <td class="gv-field-15-141">Pending</td>
                            </tr>
                            <tr>
                            <td class="gv-field-15-1">Student B</td>
                            <td class="gv-field-15-90">China</td>
                            <td class="gv-field-15-150">&nbsp;</td>
                            <td class="gv-field-15-139">Pending</td>
                            <td class="gv-field-15-140">Pending</td>
                            <td class="gv-field-15-141">Pending</td>
                            </tr>
                            </table>








                            share|improve this answer




























                              0














                              There are couple of issues:




                              • You have duplicate IDs. ID must be unique. In your current code, you will always get first element with necessary ID. So you should use classes instead

                              • You are not looping. Since its a table and you have to handle each row, you have to loop over all rows and based on current row, you have to check/update necessary column's value.





                              $(document).ready(function() {

                              var $tr = $('table tr');
                              $.each($tr, function(index, tr) {
                              if (
                              ($('.gv-field-15-139', tr).text() == 'Completed') ||
                              ($('.gv-field-15- 140', tr).text() == 'Completed') ||
                              ($('.gv-field-15-141', tr).text() == 'Completed')
                              ) {
                              $(".gv-field-15-150", tr).text('Completed');
                              }
                              })
                              });

                              <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
                              <table>
                              <tr>
                              <th>Student Name</th>
                              <th>Nationality</th>
                              <th>Status</th>
                              <th>Status1</th>
                              <th>Status2</th>
                              <th>Status3</th>
                              </tr>
                              <tr>
                              <td class="gv-field-15-1">Student A</td>
                              <td class="gv-field-15-90">India</td>
                              <td class="gv-field-15-150">&nbsp;</td>
                              <td class="gv-field-15-139">Completed</td>
                              <td class="gv-field-15-140">Pending</td>
                              <td class="gv-field-15-141">Pending</td>
                              </tr>
                              <tr>
                              <td class="gv-field-15-1">Student B</td>
                              <td class="gv-field-15-90">China</td>
                              <td class="gv-field-15-150">&nbsp;</td>
                              <td class="gv-field-15-139">Pending</td>
                              <td class="gv-field-15-140">Pending</td>
                              <td class="gv-field-15-141">Pending</td>
                              </tr>
                              </table>








                              share|improve this answer


























                                0












                                0








                                0







                                There are couple of issues:




                                • You have duplicate IDs. ID must be unique. In your current code, you will always get first element with necessary ID. So you should use classes instead

                                • You are not looping. Since its a table and you have to handle each row, you have to loop over all rows and based on current row, you have to check/update necessary column's value.





                                $(document).ready(function() {

                                var $tr = $('table tr');
                                $.each($tr, function(index, tr) {
                                if (
                                ($('.gv-field-15-139', tr).text() == 'Completed') ||
                                ($('.gv-field-15- 140', tr).text() == 'Completed') ||
                                ($('.gv-field-15-141', tr).text() == 'Completed')
                                ) {
                                $(".gv-field-15-150", tr).text('Completed');
                                }
                                })
                                });

                                <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
                                <table>
                                <tr>
                                <th>Student Name</th>
                                <th>Nationality</th>
                                <th>Status</th>
                                <th>Status1</th>
                                <th>Status2</th>
                                <th>Status3</th>
                                </tr>
                                <tr>
                                <td class="gv-field-15-1">Student A</td>
                                <td class="gv-field-15-90">India</td>
                                <td class="gv-field-15-150">&nbsp;</td>
                                <td class="gv-field-15-139">Completed</td>
                                <td class="gv-field-15-140">Pending</td>
                                <td class="gv-field-15-141">Pending</td>
                                </tr>
                                <tr>
                                <td class="gv-field-15-1">Student B</td>
                                <td class="gv-field-15-90">China</td>
                                <td class="gv-field-15-150">&nbsp;</td>
                                <td class="gv-field-15-139">Pending</td>
                                <td class="gv-field-15-140">Pending</td>
                                <td class="gv-field-15-141">Pending</td>
                                </tr>
                                </table>








                                share|improve this answer













                                There are couple of issues:




                                • You have duplicate IDs. ID must be unique. In your current code, you will always get first element with necessary ID. So you should use classes instead

                                • You are not looping. Since its a table and you have to handle each row, you have to loop over all rows and based on current row, you have to check/update necessary column's value.





                                $(document).ready(function() {

                                var $tr = $('table tr');
                                $.each($tr, function(index, tr) {
                                if (
                                ($('.gv-field-15-139', tr).text() == 'Completed') ||
                                ($('.gv-field-15- 140', tr).text() == 'Completed') ||
                                ($('.gv-field-15-141', tr).text() == 'Completed')
                                ) {
                                $(".gv-field-15-150", tr).text('Completed');
                                }
                                })
                                });

                                <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
                                <table>
                                <tr>
                                <th>Student Name</th>
                                <th>Nationality</th>
                                <th>Status</th>
                                <th>Status1</th>
                                <th>Status2</th>
                                <th>Status3</th>
                                </tr>
                                <tr>
                                <td class="gv-field-15-1">Student A</td>
                                <td class="gv-field-15-90">India</td>
                                <td class="gv-field-15-150">&nbsp;</td>
                                <td class="gv-field-15-139">Completed</td>
                                <td class="gv-field-15-140">Pending</td>
                                <td class="gv-field-15-141">Pending</td>
                                </tr>
                                <tr>
                                <td class="gv-field-15-1">Student B</td>
                                <td class="gv-field-15-90">China</td>
                                <td class="gv-field-15-150">&nbsp;</td>
                                <td class="gv-field-15-139">Pending</td>
                                <td class="gv-field-15-140">Pending</td>
                                <td class="gv-field-15-141">Pending</td>
                                </tr>
                                </table>








                                $(document).ready(function() {

                                var $tr = $('table tr');
                                $.each($tr, function(index, tr) {
                                if (
                                ($('.gv-field-15-139', tr).text() == 'Completed') ||
                                ($('.gv-field-15- 140', tr).text() == 'Completed') ||
                                ($('.gv-field-15-141', tr).text() == 'Completed')
                                ) {
                                $(".gv-field-15-150", tr).text('Completed');
                                }
                                })
                                });

                                <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
                                <table>
                                <tr>
                                <th>Student Name</th>
                                <th>Nationality</th>
                                <th>Status</th>
                                <th>Status1</th>
                                <th>Status2</th>
                                <th>Status3</th>
                                </tr>
                                <tr>
                                <td class="gv-field-15-1">Student A</td>
                                <td class="gv-field-15-90">India</td>
                                <td class="gv-field-15-150">&nbsp;</td>
                                <td class="gv-field-15-139">Completed</td>
                                <td class="gv-field-15-140">Pending</td>
                                <td class="gv-field-15-141">Pending</td>
                                </tr>
                                <tr>
                                <td class="gv-field-15-1">Student B</td>
                                <td class="gv-field-15-90">China</td>
                                <td class="gv-field-15-150">&nbsp;</td>
                                <td class="gv-field-15-139">Pending</td>
                                <td class="gv-field-15-140">Pending</td>
                                <td class="gv-field-15-141">Pending</td>
                                </tr>
                                </table>





                                $(document).ready(function() {

                                var $tr = $('table tr');
                                $.each($tr, function(index, tr) {
                                if (
                                ($('.gv-field-15-139', tr).text() == 'Completed') ||
                                ($('.gv-field-15- 140', tr).text() == 'Completed') ||
                                ($('.gv-field-15-141', tr).text() == 'Completed')
                                ) {
                                $(".gv-field-15-150", tr).text('Completed');
                                }
                                })
                                });

                                <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
                                <table>
                                <tr>
                                <th>Student Name</th>
                                <th>Nationality</th>
                                <th>Status</th>
                                <th>Status1</th>
                                <th>Status2</th>
                                <th>Status3</th>
                                </tr>
                                <tr>
                                <td class="gv-field-15-1">Student A</td>
                                <td class="gv-field-15-90">India</td>
                                <td class="gv-field-15-150">&nbsp;</td>
                                <td class="gv-field-15-139">Completed</td>
                                <td class="gv-field-15-140">Pending</td>
                                <td class="gv-field-15-141">Pending</td>
                                </tr>
                                <tr>
                                <td class="gv-field-15-1">Student B</td>
                                <td class="gv-field-15-90">China</td>
                                <td class="gv-field-15-150">&nbsp;</td>
                                <td class="gv-field-15-139">Pending</td>
                                <td class="gv-field-15-140">Pending</td>
                                <td class="gv-field-15-141">Pending</td>
                                </tr>
                                </table>






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Jan 3 at 9:42









                                RajeshRajesh

                                16.6k52254




                                16.6k52254























                                    0














                                    Loop through all the rows in the table except the first row(and hence => $('tr').length-1).



                                    .eq() function is used to access each element/text by index..



                                        $(document).ready(function () {
                                    for(i=0;i<$('tr').length-1;i++){
                                    if( ($('td#gv-field-15-139').eq(i).text() == 'Completed') || ($('td#gv-field-15-140').eq(i).text() == 'Completed') || ($('td#gv - field - 15 - 141').eq(i).text() == 'Completed') ) {
                                    $("td#gv-field-15-150").eq(i).text('Completed');
                                    }
                                    }
                                    });





                                    share|improve this answer






























                                      0














                                      Loop through all the rows in the table except the first row(and hence => $('tr').length-1).



                                      .eq() function is used to access each element/text by index..



                                          $(document).ready(function () {
                                      for(i=0;i<$('tr').length-1;i++){
                                      if( ($('td#gv-field-15-139').eq(i).text() == 'Completed') || ($('td#gv-field-15-140').eq(i).text() == 'Completed') || ($('td#gv - field - 15 - 141').eq(i).text() == 'Completed') ) {
                                      $("td#gv-field-15-150").eq(i).text('Completed');
                                      }
                                      }
                                      });





                                      share|improve this answer




























                                        0












                                        0








                                        0







                                        Loop through all the rows in the table except the first row(and hence => $('tr').length-1).



                                        .eq() function is used to access each element/text by index..



                                            $(document).ready(function () {
                                        for(i=0;i<$('tr').length-1;i++){
                                        if( ($('td#gv-field-15-139').eq(i).text() == 'Completed') || ($('td#gv-field-15-140').eq(i).text() == 'Completed') || ($('td#gv - field - 15 - 141').eq(i).text() == 'Completed') ) {
                                        $("td#gv-field-15-150").eq(i).text('Completed');
                                        }
                                        }
                                        });





                                        share|improve this answer















                                        Loop through all the rows in the table except the first row(and hence => $('tr').length-1).



                                        .eq() function is used to access each element/text by index..



                                            $(document).ready(function () {
                                        for(i=0;i<$('tr').length-1;i++){
                                        if( ($('td#gv-field-15-139').eq(i).text() == 'Completed') || ($('td#gv-field-15-140').eq(i).text() == 'Completed') || ($('td#gv - field - 15 - 141').eq(i).text() == 'Completed') ) {
                                        $("td#gv-field-15-150").eq(i).text('Completed');
                                        }
                                        }
                                        });






                                        share|improve this answer














                                        share|improve this answer



                                        share|improve this answer








                                        edited Jan 3 at 9:51









                                        Kiran Shahi

                                        5,12441846




                                        5,12441846










                                        answered Jan 3 at 9:47









                                        ReddyReddy

                                        11




                                        11























                                            0















                                            #IDs Must Always be Unique



                                            Having duplicated #ids makes HTML invalid, more importantly it cripples JS/jQ because it is expected that #ids are unique so once found, no efforts are made to continue for second occurrence of an #id.




                                            Changes




                                            • All #ids have been changed to .classes

                                            • The last 3 <td> of each row have a shared class: .status

                                            • The 3rd <td> of each row has a new class: .primary


                                            Reference



                                            .each()



                                            .prevAll()




                                            Demo



                                            Three lines of code...that's all you need.




                                            $('.status').each(function() {
                                            if ($(this).text() === 'Completed') {
                                            $(this).prevAll('.primary').text('Completed');
                                            }
                                            });

                                            <table>
                                            <tr>
                                            <th>Student Name</th>
                                            <th>Nationality</th>
                                            <th>Status</th>
                                            <th>Status1</th>
                                            <th>Status2</th>
                                            <th>Status3</th>
                                            </tr>
                                            <tr>
                                            <td class="gv-field-15-1">Student A</td>
                                            <td class="gv-field-15-90">India</td>
                                            <td class="gv-field-15-150 primary">&nbsp;</td>
                                            <td class="gv-field-15-139 status">Completed</td>
                                            <td class="gv-field-15-140 status">Pending</td>
                                            <td class="gv-field-15-141 status">Pending</td>
                                            </tr>
                                            <tr>
                                            <td class="gv-field-15-1">Student B</td>
                                            <td class="gv-field-15-90">China</td>
                                            <td class="gv-field-15-150 primary">&nbsp;</td>
                                            <td class="gv-field-15-139 status">Completed</td>
                                            <td class="gv-field-15-140 status">Pending</td>
                                            <td class="gv-field-15-141 status">Pending</td>
                                            </tr>
                                            <tr>
                                            <td class="gv-field-15-1">Student C</td>
                                            <td class="gv-field-15-90">USA</td>
                                            <td class="gv-field-15-150 primary">&nbsp;</td>
                                            <td class="gv-field-15-139 status">ERROR</td>
                                            <td class="gv-field-15-140 status">Pending</td>
                                            <td class="gv-field-15-141 status">Pending</td>
                                            </tr>
                                            <tr>
                                            <td class="gv-field-15-1">Student D</td>
                                            <td class="gv-field-15-90">France</td>
                                            <td class="gv-field-15-150 primary">&nbsp;</td>
                                            <td class="gv-field-15-139 status">Pending</td>
                                            <td class="gv-field-15-140 status">Pending</td>
                                            <td class="gv-field-15-141 status">Completed</td>
                                            </tr>
                                            </table>

                                            <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>








                                            share|improve this answer






























                                              0















                                              #IDs Must Always be Unique



                                              Having duplicated #ids makes HTML invalid, more importantly it cripples JS/jQ because it is expected that #ids are unique so once found, no efforts are made to continue for second occurrence of an #id.




                                              Changes




                                              • All #ids have been changed to .classes

                                              • The last 3 <td> of each row have a shared class: .status

                                              • The 3rd <td> of each row has a new class: .primary


                                              Reference



                                              .each()



                                              .prevAll()




                                              Demo



                                              Three lines of code...that's all you need.




                                              $('.status').each(function() {
                                              if ($(this).text() === 'Completed') {
                                              $(this).prevAll('.primary').text('Completed');
                                              }
                                              });

                                              <table>
                                              <tr>
                                              <th>Student Name</th>
                                              <th>Nationality</th>
                                              <th>Status</th>
                                              <th>Status1</th>
                                              <th>Status2</th>
                                              <th>Status3</th>
                                              </tr>
                                              <tr>
                                              <td class="gv-field-15-1">Student A</td>
                                              <td class="gv-field-15-90">India</td>
                                              <td class="gv-field-15-150 primary">&nbsp;</td>
                                              <td class="gv-field-15-139 status">Completed</td>
                                              <td class="gv-field-15-140 status">Pending</td>
                                              <td class="gv-field-15-141 status">Pending</td>
                                              </tr>
                                              <tr>
                                              <td class="gv-field-15-1">Student B</td>
                                              <td class="gv-field-15-90">China</td>
                                              <td class="gv-field-15-150 primary">&nbsp;</td>
                                              <td class="gv-field-15-139 status">Completed</td>
                                              <td class="gv-field-15-140 status">Pending</td>
                                              <td class="gv-field-15-141 status">Pending</td>
                                              </tr>
                                              <tr>
                                              <td class="gv-field-15-1">Student C</td>
                                              <td class="gv-field-15-90">USA</td>
                                              <td class="gv-field-15-150 primary">&nbsp;</td>
                                              <td class="gv-field-15-139 status">ERROR</td>
                                              <td class="gv-field-15-140 status">Pending</td>
                                              <td class="gv-field-15-141 status">Pending</td>
                                              </tr>
                                              <tr>
                                              <td class="gv-field-15-1">Student D</td>
                                              <td class="gv-field-15-90">France</td>
                                              <td class="gv-field-15-150 primary">&nbsp;</td>
                                              <td class="gv-field-15-139 status">Pending</td>
                                              <td class="gv-field-15-140 status">Pending</td>
                                              <td class="gv-field-15-141 status">Completed</td>
                                              </tr>
                                              </table>

                                              <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>








                                              share|improve this answer




























                                                0












                                                0








                                                0








                                                #IDs Must Always be Unique



                                                Having duplicated #ids makes HTML invalid, more importantly it cripples JS/jQ because it is expected that #ids are unique so once found, no efforts are made to continue for second occurrence of an #id.




                                                Changes




                                                • All #ids have been changed to .classes

                                                • The last 3 <td> of each row have a shared class: .status

                                                • The 3rd <td> of each row has a new class: .primary


                                                Reference



                                                .each()



                                                .prevAll()




                                                Demo



                                                Three lines of code...that's all you need.




                                                $('.status').each(function() {
                                                if ($(this).text() === 'Completed') {
                                                $(this).prevAll('.primary').text('Completed');
                                                }
                                                });

                                                <table>
                                                <tr>
                                                <th>Student Name</th>
                                                <th>Nationality</th>
                                                <th>Status</th>
                                                <th>Status1</th>
                                                <th>Status2</th>
                                                <th>Status3</th>
                                                </tr>
                                                <tr>
                                                <td class="gv-field-15-1">Student A</td>
                                                <td class="gv-field-15-90">India</td>
                                                <td class="gv-field-15-150 primary">&nbsp;</td>
                                                <td class="gv-field-15-139 status">Completed</td>
                                                <td class="gv-field-15-140 status">Pending</td>
                                                <td class="gv-field-15-141 status">Pending</td>
                                                </tr>
                                                <tr>
                                                <td class="gv-field-15-1">Student B</td>
                                                <td class="gv-field-15-90">China</td>
                                                <td class="gv-field-15-150 primary">&nbsp;</td>
                                                <td class="gv-field-15-139 status">Completed</td>
                                                <td class="gv-field-15-140 status">Pending</td>
                                                <td class="gv-field-15-141 status">Pending</td>
                                                </tr>
                                                <tr>
                                                <td class="gv-field-15-1">Student C</td>
                                                <td class="gv-field-15-90">USA</td>
                                                <td class="gv-field-15-150 primary">&nbsp;</td>
                                                <td class="gv-field-15-139 status">ERROR</td>
                                                <td class="gv-field-15-140 status">Pending</td>
                                                <td class="gv-field-15-141 status">Pending</td>
                                                </tr>
                                                <tr>
                                                <td class="gv-field-15-1">Student D</td>
                                                <td class="gv-field-15-90">France</td>
                                                <td class="gv-field-15-150 primary">&nbsp;</td>
                                                <td class="gv-field-15-139 status">Pending</td>
                                                <td class="gv-field-15-140 status">Pending</td>
                                                <td class="gv-field-15-141 status">Completed</td>
                                                </tr>
                                                </table>

                                                <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>








                                                share|improve this answer
















                                                #IDs Must Always be Unique



                                                Having duplicated #ids makes HTML invalid, more importantly it cripples JS/jQ because it is expected that #ids are unique so once found, no efforts are made to continue for second occurrence of an #id.




                                                Changes




                                                • All #ids have been changed to .classes

                                                • The last 3 <td> of each row have a shared class: .status

                                                • The 3rd <td> of each row has a new class: .primary


                                                Reference



                                                .each()



                                                .prevAll()




                                                Demo



                                                Three lines of code...that's all you need.




                                                $('.status').each(function() {
                                                if ($(this).text() === 'Completed') {
                                                $(this).prevAll('.primary').text('Completed');
                                                }
                                                });

                                                <table>
                                                <tr>
                                                <th>Student Name</th>
                                                <th>Nationality</th>
                                                <th>Status</th>
                                                <th>Status1</th>
                                                <th>Status2</th>
                                                <th>Status3</th>
                                                </tr>
                                                <tr>
                                                <td class="gv-field-15-1">Student A</td>
                                                <td class="gv-field-15-90">India</td>
                                                <td class="gv-field-15-150 primary">&nbsp;</td>
                                                <td class="gv-field-15-139 status">Completed</td>
                                                <td class="gv-field-15-140 status">Pending</td>
                                                <td class="gv-field-15-141 status">Pending</td>
                                                </tr>
                                                <tr>
                                                <td class="gv-field-15-1">Student B</td>
                                                <td class="gv-field-15-90">China</td>
                                                <td class="gv-field-15-150 primary">&nbsp;</td>
                                                <td class="gv-field-15-139 status">Completed</td>
                                                <td class="gv-field-15-140 status">Pending</td>
                                                <td class="gv-field-15-141 status">Pending</td>
                                                </tr>
                                                <tr>
                                                <td class="gv-field-15-1">Student C</td>
                                                <td class="gv-field-15-90">USA</td>
                                                <td class="gv-field-15-150 primary">&nbsp;</td>
                                                <td class="gv-field-15-139 status">ERROR</td>
                                                <td class="gv-field-15-140 status">Pending</td>
                                                <td class="gv-field-15-141 status">Pending</td>
                                                </tr>
                                                <tr>
                                                <td class="gv-field-15-1">Student D</td>
                                                <td class="gv-field-15-90">France</td>
                                                <td class="gv-field-15-150 primary">&nbsp;</td>
                                                <td class="gv-field-15-139 status">Pending</td>
                                                <td class="gv-field-15-140 status">Pending</td>
                                                <td class="gv-field-15-141 status">Completed</td>
                                                </tr>
                                                </table>

                                                <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>








                                                $('.status').each(function() {
                                                if ($(this).text() === 'Completed') {
                                                $(this).prevAll('.primary').text('Completed');
                                                }
                                                });

                                                <table>
                                                <tr>
                                                <th>Student Name</th>
                                                <th>Nationality</th>
                                                <th>Status</th>
                                                <th>Status1</th>
                                                <th>Status2</th>
                                                <th>Status3</th>
                                                </tr>
                                                <tr>
                                                <td class="gv-field-15-1">Student A</td>
                                                <td class="gv-field-15-90">India</td>
                                                <td class="gv-field-15-150 primary">&nbsp;</td>
                                                <td class="gv-field-15-139 status">Completed</td>
                                                <td class="gv-field-15-140 status">Pending</td>
                                                <td class="gv-field-15-141 status">Pending</td>
                                                </tr>
                                                <tr>
                                                <td class="gv-field-15-1">Student B</td>
                                                <td class="gv-field-15-90">China</td>
                                                <td class="gv-field-15-150 primary">&nbsp;</td>
                                                <td class="gv-field-15-139 status">Completed</td>
                                                <td class="gv-field-15-140 status">Pending</td>
                                                <td class="gv-field-15-141 status">Pending</td>
                                                </tr>
                                                <tr>
                                                <td class="gv-field-15-1">Student C</td>
                                                <td class="gv-field-15-90">USA</td>
                                                <td class="gv-field-15-150 primary">&nbsp;</td>
                                                <td class="gv-field-15-139 status">ERROR</td>
                                                <td class="gv-field-15-140 status">Pending</td>
                                                <td class="gv-field-15-141 status">Pending</td>
                                                </tr>
                                                <tr>
                                                <td class="gv-field-15-1">Student D</td>
                                                <td class="gv-field-15-90">France</td>
                                                <td class="gv-field-15-150 primary">&nbsp;</td>
                                                <td class="gv-field-15-139 status">Pending</td>
                                                <td class="gv-field-15-140 status">Pending</td>
                                                <td class="gv-field-15-141 status">Completed</td>
                                                </tr>
                                                </table>

                                                <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>





                                                $('.status').each(function() {
                                                if ($(this).text() === 'Completed') {
                                                $(this).prevAll('.primary').text('Completed');
                                                }
                                                });

                                                <table>
                                                <tr>
                                                <th>Student Name</th>
                                                <th>Nationality</th>
                                                <th>Status</th>
                                                <th>Status1</th>
                                                <th>Status2</th>
                                                <th>Status3</th>
                                                </tr>
                                                <tr>
                                                <td class="gv-field-15-1">Student A</td>
                                                <td class="gv-field-15-90">India</td>
                                                <td class="gv-field-15-150 primary">&nbsp;</td>
                                                <td class="gv-field-15-139 status">Completed</td>
                                                <td class="gv-field-15-140 status">Pending</td>
                                                <td class="gv-field-15-141 status">Pending</td>
                                                </tr>
                                                <tr>
                                                <td class="gv-field-15-1">Student B</td>
                                                <td class="gv-field-15-90">China</td>
                                                <td class="gv-field-15-150 primary">&nbsp;</td>
                                                <td class="gv-field-15-139 status">Completed</td>
                                                <td class="gv-field-15-140 status">Pending</td>
                                                <td class="gv-field-15-141 status">Pending</td>
                                                </tr>
                                                <tr>
                                                <td class="gv-field-15-1">Student C</td>
                                                <td class="gv-field-15-90">USA</td>
                                                <td class="gv-field-15-150 primary">&nbsp;</td>
                                                <td class="gv-field-15-139 status">ERROR</td>
                                                <td class="gv-field-15-140 status">Pending</td>
                                                <td class="gv-field-15-141 status">Pending</td>
                                                </tr>
                                                <tr>
                                                <td class="gv-field-15-1">Student D</td>
                                                <td class="gv-field-15-90">France</td>
                                                <td class="gv-field-15-150 primary">&nbsp;</td>
                                                <td class="gv-field-15-139 status">Pending</td>
                                                <td class="gv-field-15-140 status">Pending</td>
                                                <td class="gv-field-15-141 status">Completed</td>
                                                </tr>
                                                </table>

                                                <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>






                                                share|improve this answer














                                                share|improve this answer



                                                share|improve this answer








                                                edited Jan 3 at 10:19

























                                                answered Jan 3 at 10:05









                                                zer00nezer00ne

                                                25.5k32547




                                                25.5k32547






























                                                    draft saved

                                                    draft discarded




















































                                                    Thanks for contributing an answer to Stack Overflow!


                                                    • Please be sure to answer the question. Provide details and share your research!

                                                    But avoid



                                                    • Asking for help, clarification, or responding to other answers.

                                                    • Making statements based on opinion; back them up with references or personal experience.


                                                    To learn more, see our tips on writing great answers.




                                                    draft saved


                                                    draft discarded














                                                    StackExchange.ready(
                                                    function () {
                                                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54019365%2fjs-loop-through-table-and-print-text-in-column-based-on-condition%23new-answer', 'question_page');
                                                    }
                                                    );

                                                    Post as a guest















                                                    Required, but never shown





















































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown

































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown







                                                    Popular posts from this blog

                                                    android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

                                                    SQL update select statement

                                                    'app-layout' is not a known element: how to share Component with different Modules