jQuery - trigger toggle on a table element from button in a different table via parents()
I'm trying to toggle (hide/show) a table when clicking a button which is located in a different table, but have trouble selecting it correctly. I intentionnally left id tags out, as I want the jQuery code to be generic because I'll need to reuse it v开发者_StackOverflow中文版arious times in the same script.
here is where I have got so far:
http://jsfiddle.net/Argoron/Dp2sk/24/
$(document).ready(function() {
$('button.new_disp').toggle(
function() {
$(this).closest('table').next('table').hide();
$(this).text('Show');
}, function() {
debugger;
$(this).closest('table').next('table').show();
$(this).text('Hide');
});
});
Try this:
From td
-> get first parent table
-> get next sibling table
-> show/hide:
$('button.new_disp').toggle(
function() {
$(this).parents('table').next('table').hide();
$(this).text('Show');
}, function() {
$(this).parents('table').next('table').show();
$(this).text('Hide');
});
精彩评论