title: Retrieve previous and next rows in a table using jQuery
body: <p>I am using ASP.NET to create a page with a GridView that is very editable for the end user. Basically, all rows contain TextBoxes (instead of static text) which the user can edit whenever he wants. No postback occurs ever, except when he clicks the Save button on the bottom of the page. Then, I loop through each row in the grid, retrieve the values in the controls in each row and save them back to the database.</p>
  
  <p>I am now trying to implement swapping two rows (to move a row up or down) in Javascript, and for that I need some way to retrieve the previous and next rows in a table.</p>
  
  <p>At the moment my GridView contains a HiddenField as the first column, which contains the unique Id of the data item (I need this to store it back to the database of course). For other purposes (deleting a row) I have figured out how to retrieve this Id, which is this:</p>
  
  <pre><code>var itemHf = $(this).parent().parent().parent().find(':input');
  </code></pre>
  
  <p>There's so many 'parent' calls because this happens in the click event of an Image, which is inside a LinkButton, which is inside the last column of my grid. So the first parent is the LinkButton, the next is the table cell, and finally the table row. Then, I suppose the    <code>find(':input')</code> function returns the first input element in this row (which in my case is the hidden field that contains the id).</p>
  
  <p>So, using the same method I can get the <i>current</i> row:</p>
  
  <pre><code>var row = $(this).parent().parent().parent();
  </code></pre>
  
  <p>But how do I get the previous and next rows?</p>
  
  <p>Also, once I have those rows, I will need to retrieve values from more input elements. I know I can find the first one using <code>find(':input')</code>, but how do I find the second and the third input elements in this table row?</p>
  
  <p>EDIT<br>
  I don't have any html at this time but here is the ASP.NET markup for the grid:</p>
  
  <pre><code>        &lt;asp:GridView runat="server" ID="grid" AutoGenerateColumns="False" 
              onrowcommand="grid_RowCommand" onrowdeleting="grid_RowDeleting"&gt;
              &lt;Columns&gt;
  
                  &lt;!-- Move Up button --&gt;
                  &lt;asp:TemplateField&gt;
                      &lt;ItemTemplate&gt;
                          &lt;asp:LinkButton runat="server" OnClientClick="return false;"&gt;
                              &lt;asp:Image ImageUrl="~/Images/moveUp.png" AlternateText="moveUp" runat="server" CssClass="moveUp" ID="moveUp" /&gt;
                          &lt;/asp:LinkButton&gt;
                      &lt;/ItemTemplate&gt;
                  &lt;/asp:TemplateField&gt;
  
                  &lt;!-- Move Down button --&gt;
                  &lt;asp:TemplateField&gt;
                      &lt;ItemTemplate&gt;
                          &lt;asp:LinkButton runat="server" OnClientClick="return false;"&gt;
                              &lt;asp:Image ImageUrl="~/Images/moveDown.png" AlternateText="moveDown" runat="server" CssClass="moveDown" ID="moveDown" /&gt;
                          &lt;/asp:LinkButton&gt;
                      &lt;/ItemTemplate&gt;
                  &lt;/asp:TemplateField&gt;
  
                  &lt;!-- ID Hidden Field --&gt;
                  &lt;asp:TemplateField&gt;
                      &lt;ItemTemplate&gt;
                              &lt;asp:HiddenField runat="server" Value='&lt;%# DataBinder.Eval(Container.DataItem, "Id") %&gt;' /&gt;
                      &lt;/ItemTemplate&gt;
                  &lt;/asp:TemplateField&gt;
  
                  &lt;!-- Name textbox field --&gt;
                  &lt;asp:TemplateField HeaderText="Naam"&gt;
                      &lt;ItemTemplate&gt;
                              &lt;asp:TextBox runat="server" Width="200px" Text='&lt;%# DataBinder.Eval(Container.DataItem, "Name") %&gt;' /&gt;
                      &lt;/ItemTemplate&gt;
                  &lt;/asp:TemplateField&gt;
  
                  &lt;!-- Price textbox field --&gt;
                  &lt;asp:TemplateField HeaderText="Prijs"&gt;
                      &lt;ItemTemplate&gt;
                              &lt;asp:TextBox runat="server" Width="50px" Text='&lt;%# DataBinder.Eval(Container.DataItem, "Price") %&gt;' /&gt;
                      &lt;/ItemTemplate&gt;
                  &lt;/asp:TemplateField&gt;
  
                  &lt;!-- Delete button --&gt;
                  &lt;asp:TemplateField&gt;
                      &lt;ItemTemplate&gt;
                          &lt;asp:LinkButton runat="server" OnClientClick="return false;"&gt;
                              &lt;asp:Image ImageUrl="~/Images/delete.png" AlternateText="delete" runat="server" CssClass="delete" ID="delete" /&gt;
                          &lt;/asp:LinkButton&gt;
                      &lt;/ItemTemplate&gt;
                  &lt;/asp:TemplateField&gt;
              &lt;/Columns&gt;
          &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>And here is an example of the jQuery I'm using:</p>
  
  <pre><code>    $(document).ready(function () {
  
          $('table td img.delete').click(function () {
              var id = $(this).closest('tr').find(':input').val();
              alert(id);
          });
      });
  </code></pre>