Navbar links opening tabs on other pages?

Viewed 55

Hello I have a question about javascript. I have a menu link dropdown on the navbar and a technology.php page that has tabs on it.

I want it when I click the menu, for example IT Services. It will redirect to the tehcnology.php page and open the tabs and contents of the IT Services tab.

I have made the javascript code as below. But not working properly.

Is something wrong or missing? Please help me

Menu Dropdown Image

Link on the navbar

<li class="nav-item dropdown" id="technologyMenu">
    <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
         Solutions
    </a>
    <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
         <a class="dropdown-item" href="technology.php">Technology</a>
         <a class="dropdown-item" href="technology.php#datcom">Data Communication & Internet</a>
         <a class="dropdown-item" href="technology.php#it-services">IT Services</a>
         <a class="dropdown-item" href="technology.php#managed-services">Managed Services</a>
         <a class="dropdown-item" href="technology.php#smart-city">Smart City</a>
    </div>
</li>

Tabs Image

Tabs on page technology.php

<div class="navigations-tab">
   <div class="menu-tab">
       //Menu Tab
       <div class="tab-button active" id="technology">
            <span>All Technology</span>
       </div>
       <div class="tab-button" id="datcom">
            <span>Data Communication & Internet</span>
       </div>
       <div class="tab-button" id="it-services">
            <span>IT Services</span>
       </div>
       <div class="tab-button" id="managed-services">
            <span>Managed Services</span>
       </div>
       <div class="tab-button" id="smart-city">
            <span>Smart City</span>
       </div>
    </div>

    //Tab Content
    <div class="tab-box-content technology active-block">
       Tab for Tech
    </div>
    <div class="tab-box-content datcom">
       Tab for Data Communication
    </div>
    <div class="tab-box-content it-services">
       Tab for IT Services
    </div>
    <div class="tab-box-content managed-services">
       Tab for Managed Services
    </div>
    <div class="tab-box-content smart-city">
       Tab for Smart City
    </div>
</div>

My Javascript

this works for all pages,so I put this code in the footer.php file. I separate several sections such as header, footer

  var hash = window.location.hash;
  
  if (hash != "") {
    
    $('.tab-button').each(function() {
      $(this).removeClass('active');
    });

    $('.tab-box-content').each(function() {
      $(this).removeClass('active');
    });
    
    var link = "";
    $('.tab-button').each(function() {
      link = $(this).attr('id');
      if (link == hash) {
        $(this).addClass('active');
      }
    });

    $('.tab-box-content').each(function() {
      link = $(this).attr('id');
      if ('.'+link == hash) {
        $(this).addClass('active-block');
      }
    });
  }
1 Answers

The problem is $(this).attr('id') return only the id while window.location.hash also includes the "#" character.

I simplified it a bit:

// I have to force a hash for the example here to work
location.hash = "it-services";

// As well as get the hash I remove "#" character. It will make things easier
var hash = window.location.hash.slice(1);

if (hash > "") {
  $('.tab-button').removeClass('active');
  $('.tab-box-content').removeClass('active-block');
  $(`#${hash}`).addClass('active');
  $(`.${hash}`).addClass('active-block');
}
.active, .active-block {
  background: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="navigations-tab">
   <div class="menu-tab">
       //Menu Tab
       <div class="tab-button active" id="technology">
            <span>All Technology</span>
       </div>
       <div class="tab-button" id="datcom">
            <span>Data Communication & Internet</span>
       </div>
       <div class="tab-button" id="it-services">
            <span>IT Services</span>
       </div>
       <div class="tab-button" id="managed-services">
            <span>Managed Services</span>
       </div>
       <div class="tab-button" id="smart-city">
            <span>Smart City</span>
       </div>
    </div>

    //Tab Content
    <div class="tab-box-content technology active-block">
       Tab for Tech
    </div>
    <div class="tab-box-content datcom">
       Tab for Data Communication
    </div>
    <div class="tab-box-content it-services">
       Tab for IT Services
    </div>
    <div class="tab-box-content managed-services">
       Tab for Managed Services
    </div>
    <div class="tab-box-content smart-city">
       Tab for Smart City
    </div>
</div>

Related