Looping through table and changing values in jQuery

This is my tablestructure in jsp

<table id="servicetable">
<tr>
<td colspan="2"><label id="servicename">somelabel</label></td>
<td><label id="status">RUNNING </label></td>
</tr>
<tr>
<td colspan="2"><label id="servicename2">somelabel</label></td>
<td><label id="status">RUNNING </label></td>
</tr>
<tr>
<td colspan="2"><label id="servicename3">somelabel</label></td>
<td><label id="status">RUNNING </label></td>
</tr>
</table>

Now I have to change the value of "RUNNING" to "STOPPED" if my value of "servicename" first td matches another string. Below is the code

     var $all_td_in_a_table = $("#servicetable td:nth-child(1)");//to get all first td's

                  $all_td_in_a_table.each(function(){                  
                       labelText_servicename = $("#servicename").text();
                       labelText_servicename=$.trim(labelText_servicename);
                       if(labelText_servicename.toLowerCase() ==="ABC"){
                       labelText = $("#status").text();
                       $("#status").text("TEST");
                       }
});

I see the variable "all_td_in_a_table" has list of all first td's, but my .each loop is always giving me the first td. Please help.

Answers 1

  • The id attribute is supposed to be unique. Where you have duplicates, if you use $("#status") that will just select the first one. You should use classes instead. That is, change this:

    <label id="status">RUNNING </label>
    

    ...to this:

    <label class="status">RUNNING </label>
    

    As far as updating the status within the same row as the corresponding text value, you can use DOM navigation methods to stay within the same row. I would use .closest() to find the parent tr element, then .find() to go back down to that row's status.

    Note that within the .each(), you can refer to the current element with this. So all of that in context:

    $("#servicetable td:nth-child(1)").each(function() {
      var currentItem = $(this);
      if (currentItem.text().toUpperCase() === "ABC") {
        currentItem.closest("tr").find(".status").text("STOPPED");
      }
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <table id="servicetable">
      <tr>
        <td colspan="2"><label id="servicename">somelabel</label></td>
        <td><label class="status">RUNNING </label></td>
      </tr>
      <tr>
        <td colspan="2"><label id="servicename2">ABC</label></td>
        <td><label class="status">RUNNING </label></td>
      </tr>
      <tr>
        <td colspan="2"><label id="servicename3">somelabel</label></td>
        <td><label class="status">RUNNING </label></td>
      </tr>
      <tr>
        <td colspan="2"><label id="servicename2">ABC</label></td>
        <td><label class="status">RUNNING </label></td>
      </tr>
    </table>

    Note that you could also give your servicenameX fields a common class and then select by that instead of #servicetable td:nth-child(1):

    $("#servicetable .servicename").each(function() {
      // function body as above
    

    ...because then if you restructured your table to put that value in a different column you wouldn't have to change your selector. You'll notice in the working code above I don't actually use the servicenameX id values at all.

    Other than what I already covered above to get a working solution, your code didn't work because within the each loop you only ever tested the value of the first row's $("#servicename"), plus you were using .toLowerCase() and then comparing with "ABC".


Related Articles