title: javascript - loop through a table and check the value
body: <p><strong>Background Information</strong></p>
  
  <p>I have two tables, one that is a list of available users, called "users" and the other one called "selected_users".  When a row from the users table is clicked on, the app add / removes record from the selected_users table </p>
  
  <pre><code>&lt;table id=users&gt;
  &lt;tr class="odd" role="row" id="row_89"&gt;&lt;td&gt;89&lt;/td&gt;&lt;td&gt;John Doe&lt;/td&gt;&lt;td&gt;23819&lt;/td&gt;&lt;td&gt;mm2@yahoo.com&lt;/td&gt;&lt;/tr&gt;
  &lt;tr class="even" role="row" id="row_90"&gt;&lt;td&gt;90&lt;/td&gt;&lt;td&gt;John Doe&lt;/td&gt;&lt;td&gt;23819&lt;/td&gt;&lt;td&gt;36338&lt;/td&gt;&lt;/tr&gt;
  &lt;tr class="odd" role="row" id="row_91"&gt;&lt;td&gt;91&lt;/td&gt;&lt;td&gt;Jane Doe&lt;/td&gt;&lt;td&gt;23820&lt;/td&gt;&lt;td&gt;jane@yahoo.com&lt;/td&gt;&lt;/tr&gt;
  &lt;tr class="even" role="row" id="row_92"&gt;&lt;td&gt;92&lt;/td&gt;&lt;td&gt;Jane Doe&lt;/td&gt;&lt;td&gt;23820&lt;/td&gt;&lt;td&gt;28519&lt;/td&gt;&lt;/tr&gt;
  &lt;tr class="odd" role="row" id="row_93"&gt;&lt;td&gt;93&lt;/td&gt;&lt;td&gt;Jim Bob&lt;/td&gt;&lt;td&gt;23801&lt;/td&gt;&lt;td&gt;jbob@yahoo.com&lt;/td&gt;&lt;/tr&gt;
  &lt;/table&gt;
  
  &lt;table id=selected_users class="table table-condensed table-bordered" width="80%"&gt;
        &lt;tr class="info"&gt;&lt;td colspan="4"&gt;&lt;b&gt;Selected Users:&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p><strong>Question</strong></p>
  
  <p>I need to change the existing logic so that when a row in the available users list is selected, all other rows in the available users table that has a matching "pid" (which is the 3rd column) should be added to the selected_users table. </p>
  
  <p>This is the code that is triggered when someone clicks on the available users table: </p>
  
  <pre><code>    $('#users tbody').on('click', 'tr', function () {
          var id = this.id;
          var tr;
          tr=$('&lt;tr/&gt;');
  
          var index = $.inArray(id, selected);
          if ( index === -1 ) {
              selected.push( id ); //select/highlight in list of available users.
  
              // Find td's inside this tr and add to selected_users table
              var tds = $(this).find('td');
              tr.append("&lt;td&gt;" + tds.eq(0).text() + "&lt;/td&gt;");
              tr.append("&lt;td&gt;" + tds.eq(1).text() + "&lt;/td&gt;");
              tr.append("&lt;td&gt;" + tds.eq(2).text() + "&lt;/td&gt;");
              tr.append("&lt;td&gt;" + tds.eq(3).text() + "&lt;/td&gt;");
              tr. attr('id', id.substring(4));
              $('#selected_users').append(tr);
  
              //loop through the avail users table and select all records with the same p number.
              $("users td").each(function(i,o){
                  // new code goes here
              }
          } else {
              selected.splice( index, 1 ); //deselect from list of avail users
              //remove matching record from selected_users table.
              var record_id = id.substring(4);
              var rowtodelete = document.getElementById(record_id);
              rowtodelete.parentNode.removeChild(rowtodelete);
          }
          $(this).toggleClass('selected');
      } );
  } );
  </code></pre>
  
  <p><strong>What I have so far</strong></p>
  
  <p>I'm thinking of adding code like this (pseudo code) in the section with the comment "new code goes here"</p>
  
  <pre><code>       //loop through the avail users table and select all records with the same pager number.
          $("users tr").each(function(){
              $(this).find('td ....
          });
  </code></pre>
  
  <p>I'm not sure how to do a find to see if the third column matches what I have in tds.eq(2).text()</p>
  
  <p>Any suggestions would be appreciated</p>
  
  <p><strong>EDIT 1</strong>              </p>
  
  <p>This is what I have so far: </p>
  
  <pre><code>        //2015.06.11 - find and add all other rows with matching p number
              var thisTR = $(this);
              console.log(thisTR);
              //select all siblings with same value in third column
              thisTR.siblings().filter(function() {
                      console.log($('td',this).eq(2).text() );
                      //console.log($('td', thisTR).eq(2).text());
                      if ($('td',this).eq(2).text() == $('td', thisTR).eq(2).text() ) {
                              console.log("i found a match");
                              console.log("what is the row for: " + $('td',this).eq(2).text());
                      };
              });
  </code></pre>
  
  <p>I just need a way identify the row where the matching td was found, and then do something similar to what I'm already doing to add a row to selected_users: 
                  var tr;
                  tr = "";
                  ... find the row and then...
                  tr.append("" + tds.eq(0).text() + "");
                  tr.append("" + tds.eq(1).text() + "");
                  tr.append("" + tds.eq(2).text() + "");
                  tr.append("" + tds.eq(3).text() + "");
                  tr. attr('id', id.substring(4));
                  $('#selected_users').append(tr);</p>