jQuery - trigger toggle on a table element from button in a different table via parents()

368 Views Asked by At

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 various times in the same script.

here is where I have got so far:

http://jsfiddle.net/Argoron/Dp2sk/24/

2

There are 2 best solutions below

0
On BEST ANSWER
 $(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');
    });        
 });
0
On

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');
});