Adam
Adam

Reputation: 3

How can I how one div and hide multiple others with jQuery?

I am working on page load event, one DIV will be display:block and two others are set to display:none.

Once the user clicks a button to view one of the hidden DIV's the style will switch so the hidden DIV will then be set to display:block and the other will be display: none.

I have this working currently, but I was looking to see if there was a more efficient way of approaching this.

Working Fiddle:

New fiddle...:

Any suggestions would be appreciated!

Upvotes: 0

Views: 908

Answers (4)

Jithin Kunjachan
Jithin Kunjachan

Reputation: 108

You should the following inorder to optimize your code.

  1. Should rename the "overview-btn" to "overview"
  2. Should rename the "itinerary-btn" to "itinerary"
  3. Should rename the "map-btn" to "map"

Then use the following code

jQuery(document).ready(function(){
                 
      jQuery('.tabs span').click(function(){
        //First remove the class 'active' from the .tabs of all spans
        jQuery('.tabs span').removeClass('active');
        // We will make only the clicked one active (color pink)
        jQuery(this).addClass('active');
        //hide all the .overview class 
        jQuery('.overview').hide();
        //then only the clicked tabs "id"-section 
        jQuery('#'+jQuery(this).attr('id')+"-section").show();
        
      });
      //initial setting
      jQuery('.overview').hide();
      jQuery('#overview-section').show();
    });
.pageTabs .tabs span {background-color: #797979; color: #fff!important; cursor: pointer; display: inline-block; font-size: 12px; line-height: 30px; margin: 0!important; padding: 0 12px; text-decoration: none;}
    .active {background-color: #e10981!important;}
    .content {margin: 10px 0 0 0;}
    .overview {display: block;}
    .overview .itineraryTable {width: 100%; border-collapse: collapse; border-spacing: 0; margin-bottom: 20px; width: 100%; background-color: #58595b; color: #58595b;}
    .overview .itineraryTable .titleRow {background-color: #e10981!important; border: 1px solid #e10981!important; color: #fff; font-size: 14px; padding: 4px; text-align: left;}
    .overview .itineraryTable th {color: inherit; background-color: #fff; width: 15%; border: 1px solid #bcbec0; text-align: left; padding: 4px;}
    .overview .itineraryTable td {border: 1px solid #bcbec0; background-color: #fff; color: #797979; padding: 4px; width: 95%;}
    .itinerary {display: none;}
    .itinerary .heading {color: #e10981!important; font-size: 14px; line-height: 14px;}
    .itinerary p {color: #797979; font-size: 12px; line-height: 16px; margin: 10px 0; text-align: justify;}
    .itinerary hr {background-color: #bcbec0; border: medium none; height: 1px; margin: 15px 0;}
    .map {display: none;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div class="pageTabs">
                            <div class="tabs">
                                <span id="overview" class="active">Overview</span>
                                <span id="itinerary">Full Itinerary</span>
                                <span id="map">Map</span>
                            </div>
                        </div>

                        <div class="content">
                            <div class="overview" id="overview-section" style="display:block;">
                                <p>Intro 1</p>
                                <p>Intro 2</p>
                                <p>Intro 3</p>

                            </div>
                        </div>

                        <div class="overview" id="itinerary-section">

                            <div class="heading">Day 1</div>
                            <p>blah blah blah</p>
                            <hr>

                            <div class="heading">Day 2</div>
                            <p>blah blah blah</p>
                            <hr>

                            <div class="heading">Day 3</div>
                            <p>blah blah blah</p>
                            <hr>

                            <div class="heading">Day 4</div>
                            <p>blah blah blah</p>
                            <hr>

                            <div class="heading">Day 5</div>
                            <p>blah blah blah</p>
                            <hr>

                        </div>

                        <div class="overview" id="map-section">

                            map here...

                        </div>

Please let me know , if this solves your problem

Upvotes: 0

Arathi Sreekumar
Arathi Sreekumar

Reputation: 2584

Added class tab to all the individual tabs.

Note:Changed them from span to button since they are clickable elements and would get keyboard focus this way.

Also added a data attribute which contains the id of the corresponding content section.

Added a class content-section to each content section.

<div class="pageTabs">
    <div class="tabs">
        <button id="overview-btn" class="active tab" data-content="overview-section">Overview</button>
        <button id="itinerary-btn" class="tab" data-content="itinerary-section">Full Itinerary</button>
        <button id="map-btn" class="tab" data-content="map-section">Map</button>
    </div>
</div>

<div class="content">
    <div class="overview content-section selected" id="overview-section">
        <p>Intro 1</p>
        <p>Intro 2</p>
        <p>Intro 3</p>

    </div>


    <div class="itinerary content-section" id="itinerary-section">

        <div class="heading">Day 1</div>
        <p>blah blah blah</p>
        <hr />

        <div class="heading">Day 2</div>
        <p>blah blah blah</p>
        <hr />

        <div class="heading">Day 3</div>
        <p>blah blah blah</p>
        <hr />

        <div class="heading">Day 4</div>
        <p>blah blah blah</p>
        <hr />

        <div class="heading">Day 5</div>
        <p>blah blah blah</p>
        <hr />

     </div>

     <div class="map content-section" id="map-section">

        map here...

     </div>
</div>

Added the following jquery code:

jQuery(document).ready(function(){
    $('.tab').on('click',function() {
        var $this = $(this);
        var selectedContentId = '#' + $this.data('content');
        $('.tab').removeClass('active');
        $this.addClass('active');
        $('.content-section').removeClass('selected');
        $(selectedContentId).addClass('selected');
    });
});

https://jsfiddle.net/p9bnq8dp/8/

Upvotes: 0

Tushar
Tushar

Reputation: 87233

  1. Add Common Class to all the tab headers
  2. Add data-target attribute to show the element when clicked on tab-header
  3. Group all the tab-contents inside on container

See the changes highlighted in HTML and inline comments in Javascript.

Html:

<div class="pageTabs">
    <div class="tabs">
        <span id="overview-btn" class="active tabHeader" data-target="#overview-section">Overview</span>
        //                                    ^^^^^^^^^  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        <span id="itinerary-btn" class="tabHeader" data-target="#itinerary-section">Full Itinerary</span>
        //                       ^^^^^^^^^^^^^^^^  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        <span id="map-btn" class="tabHeader" data-target="#map-section">Map</span>
        //                 ^^^^^^^^^^^^^^^^  ^^^^^^^^^^^^^^^^^^^^^^^^^^
    </div>
</div>
<div class="content">
    <div class="overview" id="overview-section">
        <p>Intro 1</p>
        <p>Intro 2</p>
        <p>Intro 3</p>
    </div>
    <div class="itinerary" id="itinerary-section">
        <div class="heading">Day 1</div>
        <p>blah blah blah</p>
        <hr>
        <div class="heading">Day 2</div>
        <p>blah blah blah</p>
        <hr>
        <div class="heading">Day 3</div>
        <p>blah blah blah</p>
        <hr>
        <div class="heading">Day 4</div>
        <p>blah blah blah</p>
        <hr>
        <div class="heading">Day 5</div>
        <p>blah blah blah</p>
        <hr>
    </div>
    <div class="map" id="map-section">map here...</div>
</div>

Javascript:

$(document).ready(function () {
    // When tab-header is clicked
    $('.tabHeader').on('click', function () {

        // Add active class to the clicked element, and remove from other tab-headings
        $(this).addClass('active').siblings().removeClass('active');

        // Get the target element show it and hide other tab-contents
        $($(this).data('target')).show().siblings().hide();
    });
});

Demo: https://jsfiddle.net/tusharj/p9bnq8dp/2/

Upvotes: 2

Nathan Schwarz
Nathan Schwarz

Reputation: 641

Put all your css changes in classes in a css file and use .addClass('yourclass') in your Jquery script (don't forget to load the css file).

for .css("display", "none") use Jquery .hide() with/without .toggle()

Upvotes: 0

Related Questions