ltdev
ltdev

Reputation: 4457

jQuery - toggle button show/hide table data

I'd like some help please.

<h2>Operating systems <span class="button small toggle-btn">Toggle</span></h2>
<table class="data" cellpadding="8" border="1">
    <thead>
        <tr>
            <th class="first" style="width:120px">
                Operating System </th>
            <th class="">
                Percentage </th>
        </tr>
    </thead>
    <tbody>

        <tr>
            <td class="">iOS 8.4</td>
            <td class="">
                <div class="ui-percentage" style="width:17%">17 %</div>
            </td>
        </tr>

        <tr class="tr even">
            <td class="">iOS 8.3</td>
            <td class="">
                <div class="ui-percentage" style="width:6%">6 %</div>
            </td>
        </tr>

        <tr>
            <td class="">iOS 8.2</td>
            <td class="">
                <div class="ui-percentage" style="width:11%">11 %</div>
            </td>
        </tr>

        <tr class="tr even">
            <td class="">iOS 8.1.3</td>
            <td class="">
                <div class="ui-percentage" style="width:11%">11 %</div>
            </td>
        </tr>

        <tr>
            <td class="">iOS 8.1</td>
            <td class="">
                <div class="ui-percentage" style="width:6%">6 %</div>
            </td>
        </tr>
    </tbody>
</table>

When the page loads for the first time I want the table to be collapsed. When I click the Toggle button I want to change the table status, so basically to expand it and collapse it again if the button is clicked again.

This is my script

<script>
    $(document).ready(function() {
        $('.toggle-btn').closest('table').hide(); // I want to target the table right after the button
        $('.toggle-btn').on('click', function(event) {
            $(this).closest('table').toggle();
        });
    });
</script>

How can I make this work correct ?

Upvotes: 0

Views: 2407

Answers (4)

Parth Trivedi
Parth Trivedi

Reputation: 3832

if you have only one table then only do

$(document).ready(function() {
        $('tbody').closest('h2').next().find("tbody").hide();
        $('.toggle-btn').on('click', function() {
            $('tbody').toggle();
        });
    });

For more then one sibling tables

You can find closest h2 and next() gives you table

$(document).ready(function() {
        $('.toggle-btn').closest('h2').next().find("tbody").hide();
        $('.toggle-btn').on('click', function() {
            $(this).closest('h2').next().find("tbody").toggle();
        });
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h2>Operating systems <span class="button small toggle-btn">Toggle</span></h2>
<table class="data" cellpadding="8" border="1">
    <thead>
        <tr>
            <th class="first" style="width:120px">
                Operating System </th>
            <th class="">
                Percentage </th>
        </tr>
    </thead>
    <tbody>

        <tr>
            <td class="">iOS 8.4</td>
            <td class="">
                <div class="ui-percentage" style="width:17%">17 %</div>
            </td>
        </tr>

        <tr class="tr even">
            <td class="">iOS 8.3</td>
            <td class="">
                <div class="ui-percentage" style="width:6%">6 %</div>
            </td>
        </tr>

        <tr>
            <td class="">iOS 8.2</td>
            <td class="">
                <div class="ui-percentage" style="width:11%">11 %</div>
            </td>
        </tr>

        <tr class="tr even">
            <td class="">iOS 8.1.3</td>
            <td class="">
                <div class="ui-percentage" style="width:11%">11 %</div>
            </td>
        </tr>

        <tr>
            <td class="">iOS 8.1</td>
            <td class="">
                <div class="ui-percentage" style="width:6%">6 %</div>
            </td>
        </tr>
    </tbody>
</table>

Upvotes: 2

Shrikantha Budya
Shrikantha Budya

Reputation: 644

why not just use toggle function directly?

$(document).ready(function() {
    $('table').hide();
    $('.toggle-btn').on('click', function() {
        $('table').toggle();
    });
});

Upvotes: 3

guradio
guradio

Reputation: 15555

$(document).ready(function() {
  $('.toggle-btn').parent().next('table').hide(); // I want to target the table right after the button
  $('.toggle-btn').on('click', function(event) {
    $(this).parent().siblings('table').toggle();
  });
});

Selector should be $('.toggle-btn').parent().next('table')

DEMO

Upvotes: 1

mikeyq6
mikeyq6

Reputation: 1148

the function closest() goes up the element's parents, so it will never find the table. You would have to do something like this:

$(document).ready(function() {
    $('.toggle-btn').parent().parent().find('table').hide(); // I want to target the table right after the button
    $('.toggle-btn').on('click', function(event) {
        $(this).parent().parent().find('table').toggle();
    });
});

But this would not work if there are multiple tables. Consider using an id or for the table, so that you can access it directly. Eg:

<table id="data-table-1" class="data" cellpadding="8" border="1">

That would make the javascript much simpler, ie:

$('.toggle-btn').on('click', function(event) {
    $('#data-table-1').toggle();
});

Upvotes: 0

Related Questions