title: JQuery, select first row of table
body: <p>I've used JQuery to add a "image" button to a few rows in a table. I used the following code:</p>
  
  <pre><code>$("#tblResults tr:nth-child(1) td.ResultsHeader span.gridRCHders").each(function(){
      var cat = $.trim($(this).text());
      if(cat.length &gt; 0){
          if(cat.toLowerCase() != "total"){
              options.xAxis.categories.push(cat);
          }
      }
  });
  </code></pre>
  
  <p>which basically goes through the first row of the table and stores the values of the cell in a categories array. I'm making changes to an existing site so can't really change the table layout.</p>
  
  <p>What I have noticed is on some pages, more than one table is displayed, my image buttons are added in correctly, but what I need to change is the above code to read the first row of the table that the image is on... I've tried the below but it doens't seem to be working:</p>
  
  <pre><code>$("img.ChartButton").click(function(){
      var currentTable = $(this).closest("tr").closest("table").filter("tr:nth-child(1) td.ResultsHeader span.gridRCHders");
  
      options.xAxis.categories = [];
      currentTable.each(function(){
          var cat = $.trim($(this).text());
          if(cat.length &gt; 0){
              if(cat.toLowerCase() != "total"){
                  options.xAxis.categories.push(cat);
              }
          }
      });
  });
  </code></pre>
  
  <p>Any help would be greatful. Thanks</p>
  
  <p>H</p>
  
  <p>EDIT:
  Please find below a copy of one of the tables, as you can see on row 2, I have a "ChartButton" image, This can be on any row from 2 onwards, and they can be more than one in the table. What I need to do is when the image is clicked that the data in row ONE of the current table (they could be more than one table on the page) is read in and saved in to the categories array (see above code). Hope this helps.</p>
  
  <pre><code> &lt;table cellspacing="1" cellpadding="2" border="0" class="whiteBorder"&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td width="40" valign="bottom" align="left" class="ResultsHeader" colspan="1" rowspan="1"&gt;&lt;span class="gridRCHders"&gt;&amp;nbsp;&lt;/span&gt;&lt;/td&gt;
              &lt;td width="40" valign="bottom" align="left" class="ResultsHeader" colspan="1" rowspan="1"&gt;&lt;span class="gridRCHders"&gt;Jan&lt;/span&gt;&lt;/td&gt;
              &lt;td width="40" valign="bottom" align="left" class="ResultsHeader" colspan="1" rowspan="1"&gt;&lt;span class="gridRCHders"&gt;Feb&lt;/span&gt;&lt;/td&gt;
              &lt;td width="40" valign="bottom" align="left" class="ResultsHeader" colspan="1" rowspan="1"&gt;&lt;span class="gridRCHders"&gt;Mar&lt;/span&gt;&lt;/td&gt;
              &lt;td width="40" valign="bottom" align="left" class="ResultsHeader" colspan="1" rowspan="1"&gt;&lt;span class="gridRCHders"&gt;Apr&lt;/span&gt;&lt;/td&gt;
              &lt;td width="40" valign="bottom" align="left" class="ResultsHeader" colspan="1" rowspan="1"&gt;&lt;span class="gridRCHders"&gt;May&lt;/span&gt;&lt;/td&gt;
              &lt;td width="40" valign="bottom" align="left" class="ResultsHeader" colspan="1" rowspan="1"&gt;&lt;span class="gridRCHders"&gt;Jun&lt;/span&gt;&lt;/td&gt;
              &lt;td width="40" valign="bottom" align="left" class="ResultsHeader" colspan="1" rowspan="1"&gt;&lt;span class="gridRCHders"&gt;Jul&lt;/span&gt;&lt;/td&gt;
              &lt;td width="40" valign="bottom" align="left" class="ResultsHeader" colspan="1" rowspan="1"&gt;&lt;span class="gridRCHders"&gt;Aug&lt;/span&gt;&lt;/td&gt;
              &lt;td width="40" valign="bottom" align="left" class="ResultsHeader" colspan="1" rowspan="1"&gt;&lt;span class="gridRCHders"&gt;Sep&lt;/span&gt;&lt;/td&gt;
              &lt;td width="40" valign="bottom" align="left" class="ResultsHeader" colspan="1" rowspan="1"&gt;&lt;span class="gridRCHders"&gt;Oct&lt;/span&gt;&lt;/td&gt;
              &lt;td width="40" valign="bottom" align="left" class="ResultsHeader" colspan="1" rowspan="1"&gt;&lt;span class="gridRCHders"&gt;Nov&lt;/span&gt;&lt;/td&gt;
              &lt;td width="40" valign="bottom" align="left" class="ResultsHeader" colspan="1" rowspan="1"&gt;&lt;span class="gridRCHders"&gt;Dec&lt;/span&gt;&lt;/td&gt;
              &lt;td width="40" valign="bottom" align="left" class="ResultsHeader" colspan="1" rowspan="1"&gt;&lt;span class="gridRCHders"&gt;Total&lt;/span&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr class="lGreyBG"&gt;
              &lt;td align="left" colspan="15"&gt;
                  &lt;span class="gridTXT"&gt;&lt;b&gt;KPI&lt;/b&gt;&amp;nbsp;&lt;img width="20" alt="Chart" src="Images/chart_bar.png" id="c0_1" class="ChartButton"&gt;&lt;/span&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr class="lGreyBGalt"&gt;
              &lt;td nowrap="" class="ResultsHeader"&gt;&lt;span class="gridRCHders"&gt;Pre Conversion %&lt;/span&gt;&lt;/td&gt;
              &lt;td nowrap="" align="right"&gt;&lt;span class="gridTXT"&gt;&lt;i&gt;65 %&lt;/i&gt;&lt;/span&gt;&lt;/td&gt;
              &lt;td nowrap="" align="right"&gt;&lt;span class="gridTXT"&gt;&lt;i&gt;65 %&lt;/i&gt;&lt;/span&gt;&lt;/td&gt;
              &lt;td nowrap="" align="right"&gt;&lt;span class="gridTXT"&gt;&lt;i&gt;66 %&lt;/i&gt;&lt;/span&gt;&lt;/td&gt;
              &lt;td nowrap="" align="right"&gt;&lt;span class="gridTXT"&gt;&lt;i&gt;62 %&lt;/i&gt;&lt;/span&gt;&lt;/td&gt;
              &lt;td nowrap="" align="right"&gt;&lt;span class="gridTXT"&gt;&lt;i&gt;67 %&lt;/i&gt;&lt;/span&gt;&lt;/td&gt;
              &lt;td nowrap="" align="right"&gt;&lt;span class="gridTXT"&gt;&lt;i&gt;67 %&lt;/i&gt;&lt;/span&gt;&lt;/td&gt;
              &lt;td nowrap="" align="right"&gt;&lt;span class="gridTXT"&gt;&lt;i&gt;68 %&lt;/i&gt;&lt;/span&gt;&lt;/td&gt;
              &lt;td nowrap="" align="right"&gt;&lt;span class="gridTXT"&gt;&lt;i&gt;69 %&lt;/i&gt;&lt;/span&gt;&lt;/td&gt;
              &lt;td nowrap="" align="right"&gt;&lt;span class="gridTXT"&gt;&lt;i&gt;0 %&lt;/i&gt;&lt;/span&gt;&lt;/td&gt;
              &lt;td nowrap="" align="right"&gt;&lt;span class="gridTXT"&gt;&lt;i&gt;0 %&lt;/i&gt;&lt;/span&gt;&lt;/td&gt;
              &lt;td nowrap="" align="right"&gt;&lt;span class="gridTXT"&gt;&lt;i&gt;0 %&lt;/i&gt;&lt;/span&gt;&lt;/td&gt;
              &lt;td nowrap="" align="right"&gt;&lt;span class="gridTXT"&gt;&lt;i&gt;0 %&lt;/i&gt;&lt;/span&gt;&lt;/td&gt;
              &lt;td nowrap="" align="right"&gt;&lt;span class="gridTXT"&gt;&lt;i&gt;66 %&lt;/i&gt;&lt;/span&gt;&lt;/td&gt;
          &lt;/tr&gt;           
      &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p><strong>EDIT:</strong>
  Finally have it working, below is the line I had to use for it to get to the right data in the first row...</p>
  
  <pre><code>var $row = $(this).closest('table').children('tbody').children('tr:first').children('td.ResultsHeader').children('span.gridRCHders');
  </code></pre>
  
  <p>I'm not sure why I needed all the children calls, as I did try the following first which <strong>didn't</strong> work:</p>
  
  <pre><code>var $row = $(this).closest('table').children('tbody').children('tr:first td.ResultsHeader span.gridRCHders');
  </code></pre>
  
  <p>Thanks to everyone below for the help.</p>