Solved

Synchronize upper and lower tabs on a form

Posted on 2016-07-22
5
43 Views
Last Modified: 2016-07-25
I have a tall form in which a user has access to three tabs on top and three corresponding tabs on the bottom.

I'm trying to write javascript\jquery to keep the tabs in sync(active) depending upon which is selected.

So here are my upper tabs:

<ul id="Tabs" class="nav nav-tabs">
        <li class="active"><a href="#pm" data-toggle="tab" id="pmTab">PM</a></li>
        <li><a href="#engineer" data-toggle="tab" id="engineerTab">Engineer</a></li>
        <li><a href="#contractor" data-toggle="tab" id="contractingTab">Contracting</a></li>
    </ul>

<div class="tab-content well"> .....

Open in new window



Here are my lower tabs
...</div>
 <div id="BottomTabs" class="tabbable tabs-below">
            <ul id="TabsBelow" class="nav nav-tabs">
                <li class="active"><a href="#pm" data-toggle="tab" id="pmTabBottom">PM</a></li>
                <li><a href="#engineer" data-toggle="tab" id="engineerTabBottom">Engineer</a></li>
                <li><a href="#contractor" data-toggle="tab" id="contractingTabBottom">Contracting</a></li>
            </ul>
        </div>

Open in new window


Now here are my click events
 function BindTabClickEvents() {
            $('#Tabs a, #TabsBelow a').click(function (e) {
                e.preventDefault();
                
                $(".nav-tabs li").each(function(element) {
                    $(this).removeClass("active");
                });

                $(this).addClass("active");
               

                switch ($(this).html()) {
                    case "PM":
                       
                        if (e.target.id === "pmTab") {
                            $('#pmTabBottom').addClass("active");
                        } else {
                            $('#pmTab').addClass("active");
                        }
                        break;
                    case "Engineer":
                       if (e.target.id === "engineerTab") {
                            $('#engineerTabBottom').addClass("active");
                        } else {
                            $('#engineerTab').addClass("active");
                        }
                        break;
                    case "Contracting":
                        $('#programNameContracting').html($("#ProgramName").val());
                        if (e.target.id === "contractingTab") {
                            $('#contractingTabBottom').addClass("active");
                        } else {
                           $('#contractingTab').addClass("active");
                        }
                        break;
                }

                $(this).tab('show');
            });
        }

Open in new window


The case statement is assigning the "Active" class but the corresponding tab does not look "active". Also, the other tabs seem to be retaining the active class even after I iterate through them and remove it.
0
Comment
Question by:Focker513
  • 2
  • 2
5 Comments
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 41725110
Can you create a working sample?  Either a minimal sample page on your own site or recreate on jsbin, jsfiddle or codepen.  

Just as a quick suggestion, on the click event the first thing you can do is make all inactive, then only make the clicked tab active.
0
 

Author Comment

by:Focker513
ID: 41725238
Sorry been pulled into another issue. I will try to update with jsfiddle later. If you look I am iterating through and removing active so you are saying also add inactive? That actually may be what I need to try.

Regardless can't test at the moment but will get back to you. I apologize, thanks for your time.
0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 41725300
Here is a simple example where I remove the class on click, then add to the specific item.
http://jsbin.com/cuzoruceve/edit?html,output
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-2.0.3.js"></script>
  <script>
    $(function(){
       $('ul#top li').on('click',function(){
          $('ul#top li').removeClass('red');
          $(this).addClass('red');
 
          });
  
    });
  </script>
  <style>
    .red{color:Red;}
    .blue{color:Blue;}
  </style>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
   <ul id="top">
     <li>one</li>
     <li>two</li>
     <li>three</li>
   </ul>
   <ul id="bottom">
     <li>one</li>
     <li class="blue">two</li>
     <li>three</li>
   </ul>
</body>
</html>

Open in new window

0
 
LVL 49

Accepted Solution

by:
Roonaan earned 500 total points
ID: 41725606
I would go for a setup like this. Instead of matching tabs by "id" I use the "href" to figure out if two tabs have the same target and thus need to be highlighted with the same state.
This will make sure you code does not expands with the number of your tabs, introducing costly maintenance.

 function BindTabClickEvents() {
    var links = $('#Tabs a, #TabsBelow a');
    links.click(function(e) {
        e.preventDefault();
        var linkClicked = $(this).closest('a');
        var target = linkClicked.attr('href');
        links.each(function() {
           if ($(this).attr('href') === target) {
                $(this).addClass('active');
                $(this).closest('li').addClass('active');
           } else {
               $(this).removeClass('active');
               $(this).closest('li').removeClass('active');
           }
        });
        $(this).tab('show');
    });
}

Open in new window


The above code does toggle "active" class on both the <a> as the surrounding <li>. From your code it was unclear which one is needed in the end.

Best

-r-
0
 

Author Closing Comment

by:Focker513
ID: 41727806
awesome worked without issue
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial viewers will learn how add a full-size background image to a webpage using CSS3. Create a new HTML document with an internal stylesheet.: In CSS, define the html element to have a background image. Use a high resolution image.: In t…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now