Question

I'm having trouble making it so if I click a button, one table becomes hidden and another is shown.

Here is a link to the DEMO

Here are the main bits of code:

<div id="container">    
    <table id="1" div id="table3">
        <div class="table_2col">
            <div class="caption">
                <ul>

            </div><!--/ column 3-->    
            <div class="clear"></div>   
        </div><!--/. table_3col-->

        <div class="table_4col">
            <div class="caption">

I'm using table id ="1" for the first table and table id="2" for the second, but doing this seems to take away the css style.

Css:

a.button{    
    color: white;
    padding: 0.5rem;
    border-radius: 5px;
    text-decoration: none;
    &:hover;
}
a.button:nth-child(1){
     background-color: #FA202B;
}

JavaScript:

(function () {
    var tables = $("table");
    //Grabs all the tables
    tables.hide().first().show();
    //Hides all the tables except first
    $("a.button").on("click", function () {
        //Adds eventListner to buttons
        tables.hide();
        //Hides all the tables
        var tableTarget = $(this).data("table");
        //Gets data# of button
        $("table#" + tableTarget).show();
        //Shows the table with an id equal to data attr of the button
    })
})();
Was it helpful?

Solution

I would set all tables to display:none; and then use jQuery Toggle Class to add an overriding "visible" class to the current table.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top