Reputation: 809
my code
<table id="mytable">
<tr>
<td>Text 5</td>
<td>Other text 5</td>
<td><input name="name5" value=""></td>
<td><input type="checkbox" value="5" name="records"></td>
</tr>
<tr>
<td>Text 7</td>
<td>Other text 7</td>
<td><input name="name7" value=""></td>
<td><input type="checkbox" value="7" name="records"></td>
</tr>
<tr>
<td>Text 13</td>
<td>Other text 13</td>
<td><input name="name13" value=""></td>
<td><input type="checkbox" value="13" name="records"></td>
</tr>
</table>
get the middle input checkbox with
$('#mytable input[type=checkbox][name=records][value=7]')
good :-)
now, how can i get the prev input () and the next () input?
i try
$('#mytable input[type=checkbox][name=records][value=2]').prev('input[type=checkbox]')
for prev, but not works :-(
thanks :-)
Upvotes: 0
Views: 4026
Reputation: 166051
Assuming you have a reference to that middle checkbox in checkbox
, and also assuming that there is only going to be one checkbox descendant of each tr
element:
//Previous checkbox:
checkbox.closest("tr").prev().find(":checkbox");
//Next checkbox:
checkbox.closest("tr").next().find(":checkbox");
This gets the ancestor tr
element with .closest()
, and then uses .prev()
or .next()
to select the previous or following tr
, and then finds any checkbox descendants of that.
Also note the use of the :checkbox
selector, which is basically a shortcut to the attribute selector you are currently using. You can use either, but the attribute selector is probably a bit faster since it's a native CSS selector.
Upvotes: 2