title: unexpected results from jQuery loop on dynamic HTML table
body: <p>I have a table that is created in <code>PHP</code>. This table is the result of a query and it generates several rows. Inside these rows are quite a few different elements, such as drop down menus (&lt; select >s) check boxes, and text fields. I made a loop to go through the elements in <code>jQuery</code> and get the values, but it's not returning what I thought it would. This is a fairly complex situation, so I'm going to post the code and a fiddle to perfectly illustrate exactly what is happening, and what I expect to happen.</p>
  
  <p><strong>PHP</strong></p>
  
  <pre><code>if(isset($_POST['aid'])) {  
      $aid = $_POST['aid'];
      $projects = getProjects($db);
      $members = getUserName($db);            
      try {
          $project_info = $db-&gt;prepare("
              SELECT  projects.id,
                      projects.project_name,                  
                      projects.pm,    
                      projects.apm,
                      projects.est_start,
                      projects.est_end,
                      projects.contact,                   
                      projects.trips,
                      projects.tasks,
                      projects.perc_complete,
                      projects.bcwp,
                      projects.actuals,
                      projects.cpi,
                      projects.bcws,
                      projects.bac,
                      projects.comments,
                      projects.status,
                      projects.project_revenue,
                      projects.profit_margin,
                      projects.pm_perc,
                      projects.audited         
              FROM projects
              WHERE account_id = ?                        
          "); 
  
          $project_info-&gt;bindValue(1, $aid, PDO::PARAM_STR);
          $project_info-&gt;execute();       
  
              echo "&lt;table class='projects_contentTable'&gt;";
              echo "&lt;thead class='projects_editable_thead'&gt;";             
              echo "&lt;th class='content_th'&gt;" . "Job #" . "&lt;/th&gt;";
              echo "&lt;th class='content_th'&gt;" . "Project Name" . "&lt;/th&gt;";
              echo "&lt;th class='content_th'&gt;" . "PM" . "&lt;/th&gt;";
              echo "&lt;th class='content_th'&gt;" . "APM" . "&lt;/th&gt;"; 
              echo "&lt;th class='content_th'&gt;" . "Est. Start" . "&lt;/th&gt;";
              echo "&lt;th class='content_th'&gt;" . "Est. End" . "&lt;/th&gt;";
              echo "&lt;th class='content_th'&gt;" . "Contact" . "&lt;/th&gt;";
              echo "&lt;th class='content_th'&gt;" . "Trips" . "&lt;/th&gt;";
              echo "&lt;th class='content_th'&gt;" . "Tasks" . "&lt;/th&gt;";
              echo "&lt;th class='content_th'&gt;" . "% Complete" . "&lt;/th&gt;";
              echo "&lt;th class='content_th'&gt;" . "BCWP" . "&lt;/th&gt;";
              echo "&lt;th class='content_th'&gt;" . "Actuals" . "&lt;/th&gt;";
              echo "&lt;th class='content_th'&gt;" . "CPI" . "&lt;/th&gt;";           
              echo "&lt;th class='content_th'&gt;" . "Comments" . "&lt;/th&gt;";  
              echo "&lt;th class='content_th'&gt;" . "Status" . "&lt;/th&gt;";    
              echo "&lt;th class='content_th'&gt;" . "Project Revenue" . "&lt;/th&gt;";   
              echo "&lt;th class='content_th'&gt;" . "Profit Margin" . "&lt;/th&gt;"; 
              echo "&lt;th class='content_th'&gt;" . "PM%" . "&lt;/th&gt;";   
              echo "&lt;th class='content_th'&gt;" . "Audited" . "&lt;/th&gt;";
              echo "&lt;/thead&gt;";
  
              echo "&lt;tbody class='projects_editable_tbody'&gt;";                  
              while ($row = $project_info-&gt;fetch(PDO::FETCH_ASSOC)) {             
                  echo "&lt;tr&gt;";                            
                  echo "&lt;td class='projects_editable_content_td'&gt;" . "&lt;a href='#'&gt;" . $row['id'] . "&lt;/a&gt;" . "&lt;/td&gt;";
                  echo "&lt;td class='projects_editable_content_td' contenteditable='true'&gt;" . $row['project_name'] . "&lt;/td&gt;";
                  echo "&lt;td class='projects_editable_content_td'&gt;" . 
                          "&lt;select id='table_edit_project_pm'&gt;" . 
                              "&lt;option value=''&gt;" . $row['pm'] . "&lt;/option&gt;" . 
                              "&lt;option&gt;" . "-----" . "&lt;/option&gt;";
                          foreach($members as $key =&gt; $value) {
                              echo "&lt;option value='".$key."'&gt;" . $value . "&lt;/option&gt;";            
                          } 
                  echo "&lt;/select&gt;";
                  echo "&lt;/td&gt;";               
                  echo "&lt;td class='projects_editable_content_td'&gt;" . 
                          "&lt;select id='table_edit_project_apm'&gt;" . 
                              "&lt;option value=''&gt;" . $row['apm'] . "&lt;/option&gt;" . 
                              "&lt;option&gt;" . "-----" . "&lt;/option&gt;";
                          foreach($members as $key =&gt; $value) {
                              echo "&lt;option value='".$key."'&gt;" . $value . "&lt;/option&gt;";            
                          } 
                  echo "&lt;/select&gt;";
                  echo "&lt;/td&gt;";               
                  echo "&lt;td class='projects_editable_content_td'&gt;" . 
                      "&lt;input type='text' id='table_edit_project_start' value='". $row['est_start'] ."'/&gt;"; 
                  echo "&lt;/td&gt;";               
                  echo "&lt;td class='projects_editable_content_td'&gt;" . 
                      "&lt;input type='text' id='table_edit_project_end' value='". $row['est_end'] ."'/&gt;";
                  echo "&lt;/td&gt;";               
                  echo "&lt;td class='projects_editable_content_td' contenteditable='true'&gt;" . $row['contact'] . "&lt;/td&gt;";
                  echo "&lt;td class='content_td'&gt;" . $row['trips'] . "&lt;/td&gt;";
                  echo "&lt;td class='content_td'&gt;" . $row['tasks'] . "&lt;/td&gt;";
                  echo "&lt;td class='content_td'&gt;" . $row['perc_complete'] . "&lt;/td&gt;";
                  echo "&lt;td class='content_td'&gt;" . $row['bcwp'] . "&lt;/td&gt;";
                  echo "&lt;td class='content_td'&gt;" . $row['actuals'] . "&lt;/td&gt;";
                  echo "&lt;td class='content_td'&gt;" . $row['cpi'] . "&lt;/td&gt;";             
                  echo "&lt;td class='projects_editable_content_td' contenteditable='true'&gt;" . $row['comments'] . "&lt;/td&gt;";
                  echo "&lt;td class='projects_editable_content_td'&gt;" .
                          "&lt;select&gt;" .
                              "&lt;option value=''&gt;" . $row['status'] . "&lt;/option&gt;" .
                              "&lt;option&gt;" . "-------" . "&lt;/option&gt;" .
                              "&lt;option&gt;" . "On Hold" . "&lt;/option&gt;" .
                              "&lt;option&gt;" . "In Progress" . "&lt;/option&gt;" .
                              "&lt;option&gt;" . "Open" . "&lt;/option&gt;" .
                              "&lt;option&gt;" . "Complete" . "&lt;/option&gt;" .
                           "&lt;/select&gt;"; 
                  echo "&lt;/td&gt;";
                  echo "&lt;td class='content_td'&gt;" . $row['project_revenue'] . "&lt;/td&gt;";
                  echo "&lt;td class='content_td'&gt;" . $row['profit_margin'] . "&lt;/td&gt;";
                  echo "&lt;td class='content_td'&gt;" . $row['pm_perc'] . "&lt;/td&gt;";
                  echo "&lt;td class='projects_editable_content_td'&gt;";                                   
                      if ($row['audited'] == 'true') {
                          echo "&lt;input type='checkbox' checked='checked'/&gt;";
                      } else {
                          echo "&lt;input type='checkbox'/&gt;";
                      }
                  echo "&lt;/td&gt;";               
                  echo "&lt;/tr&gt;";               
              }
              echo "&lt;/tbody&gt;";    
              echo "&lt;/table&gt;";                
      } catch(PDOException $e) {
          die($e-&gt;getMessage());
      }   
  } else {    
      echo 'could not load projects table';
  }
  </code></pre>
  
  <p><strong>jQuery</strong> </p>
  
  <pre><code>$('.projects_editable_tbody tr').each(function() {
          $('.projects_editable_content_td:not(:has(select)),option:selected').each(function() {
              saveEdits.push($(this).text());
          });
      }); 
  
      $.each(saveEdits, function(index, value) { 
          alert(index + ': ' + value); 
      });
  </code></pre>
  
  <p>OK end wall, so here is the fiddle to play with <a href="http://jsfiddle.net/U3nna/14/" rel="nofollow">http://jsfiddle.net/U3nna/14/</a></p>
  
  <p>Inside the fiddle I actually list the values I'm expecting and all you have to do is click the save button to see what is actually happening. </p>
  
  <p>For those of you who don't want to use the fiddle, or perhaps can't here is your very brief summary.</p>
  
  <p>I need to loop through the table and go through each table row. Inside each table row I need the text value from the &lt; td >'s with the class " .projects_editable_content_td". Each of these values will be put into an array. <em>However</em> some of the values are not being stored, and also it seems that this loop does an extra iteration on the table. So sometimes I'm expecting 9 or 18 values, and I end up with 27 or 36, and the more I run it the worse it gets.</p>
  
  <p>I'm wracking my brain but I can't seem to figure out why it wouldn't be picking up some of these elements text values. If anyone has the time to lend me a hand, i'd really appreciate it.</p>