Javanewbie
Javanewbie

Reputation: 73

Tabs using Div in HTML

I am trying to create sub tabs using DIV.
I can view the tabs and sub tabs with the code. Also clicking on main tab is working. But unfortunately sub tabs are not working, It seems like the code issue . I am struggling to find the issue. any help will be highly appreciated. thanks in advance.

Code is actually based on the W3 schools w3schools

function openCity(evt, cityName) {
    var i, tabcontent, tablinks;
    tabcontent = document.getElementsByClassName("tabcontent");
    for (i = 0; i < tabcontent.length; i++) {
        tabcontent[i].style.display = "none";
    }
    tablinks = document.getElementsByClassName("tablinks");
    for (i = 0; i < tablinks.length; i++) {
        tablinks[i].className = tablinks[i].className.replace(" active", "");

    }
    document.getElementById(cityName).style.display = "block";
  


    evt.currentTarget.className += " active";
}

var name = '0';

document.getElementById(name).click();
body {
       background-image: url("Images/MainBackground.jpeg");
    background-color: #2980B9;
        background-size: 100% 750px;

  font-family: 'Roboto', sans-serif;
  
}

    
    
    /* Style the tab */
.tab {
    overflow: hidden;
    border: 1px solid #ccc;
    background-color: #f1f1f1;
}

/* Style the buttons inside the tab */
.tab button {
    background-color: inherit;
    float: left;
    border: none;
    outline: none;
    cursor: pointer;
    padding: 14px 16px;
    transition: 0.3s;
    font-size: 17px;
}

/* Change background color of buttons on hover */
.tab button:hover {
    background-color: #ddd;
}

/* Create an active/current tablink class */
.tab button.active {
    background-color: #ccc;
}

/* Style the tab content */
.tabcontent {
    display: none;
    padding: 6px 12px;
    -webkit-animation: fadeEffect 1s;
    animation: fadeEffect 1s;
}

/* Fade in tabs */
@-webkit-keyframes fadeEffect {
    from {opacity: 0;}
    to {opacity: 1;}
}

@keyframes fadeEffect {
    from {opacity: 0;}
    to {opacity: 1;}
}

#main {
    width: 70px;
    height: 300px;
    border: 1px solid #c3c3c3;
    display: -webkit-flex;
    display: flex;
    -webkit-flex-wrap: wrap;
    flex-wrap: wrap;
    -webkit-align-content: center;
    align-content: center;
}

#main div {
    width: 70px;
    height: 70px;
}
<div class="tab">
     <button class="tablinks" id='0' onclick="openCity(event, 'Sales')">Sales</button> 
    
      <button class="tablinks" id='1' onclick="openCity(event, 'Costs')">Costs</button>
            <button class="tablinks" id='2' onclick="openCity(event, 'Expenses')">Expenses</button>
        <button class="tablinks" id='3' onclick="openCity(event, 'Inventory')">Inventory</button>
  <button class="tablinks" id='4' onclick="openCity(event, 'Products')">Products</button>
    <button class="tablinks" id='5' onclick="openCity(event, 'Customers')">Customers</button> 
   <button class="tablinks" id='6' onclick="openCity(event, 'Performance Reports')">Business Insights</button>
      <button class="tablinks" id='7' onclick="openCity(event, 'Administration')">Administration</button>

  </div>

  
<div id="Products" class="tabcontent">    
  <h3>Products</h3>
  
       
      
       
      </div>
<div id="Sales" class="tabcontent">    
  <h3>Sales</h3>
  
     
       
      <div class="tab">
  <button class="tablinks" onclick="openCity(event, 'SaleDetail')">Sale Detail</button>
  <button class="tablinks" onclick="openCity(event, 'SaleSummary')">Sale Summary</button>
  <button class="tablinks" onclick="openCity(event, 'SaleHistory')">Sale History</button>
</div>

<div id="SaleDetail" class="tabcontent">
 test     
</div>

<div id="SaleSummary" class="tabcontent">
    
test2       
</div>
<div id="SaleHistory" class="tabcontent">
Test3
</div>
       
       
       
       <br>
       
       
       
       
</div>

<div id="Customers" class="tabcontent">
  <h3>Customers</h3>
  
  
    

       </div>

<div id="Costs" class="tabcontent">
  <h3>Costs</h3>
  
    
   
</div>
<div id="Expenses" class="tabcontent">
  <h3>Expenses</h3>
  
  
</div>
<div id="Inventory" class="tabcontent">
  <h3>Inventory</h3>
     
       
      
       
</div>
<div id="PerformanceReports" class="tabcontent">
  <h3>Performance Reports</h3>
Analyze business performance
</div>
       
       
       
       <div id="Administration" class="tabcontent">
  <h3>Administration</h3>
 
       
       
       
</div>

Upvotes: 0

Views: 2707

Answers (1)

David
David

Reputation: 377

You need to close a div after the three buttons inside sales:

    ...
  <button class="tablinks" onclick="openCity(event, 'SaleDetail')">Sale Detail</button>
  <button class="tablinks" onclick="openCity(event, 'SaleSummary')">Sale Summary</button>
  <button class="tablinks" onclick="openCity(event, 'SaleHistory')">Sale History</button>
  </div></div>

You just missed one

   </div>

Here and put one in the wrong place later, your Sales div must be closed before putting the content divs about SaleDetail, SaleSummary..

Updated code:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {font-family: Arial;}

/* Style the tab */
.tab {
    overflow: hidden;
    border: 1px solid #ccc;
    background-color: #f1f1f1;
}

/* Style the buttons inside the tab */
.tab button {
    background-color: inherit;
    float: left;
    border: none;
    outline: none;
    cursor: pointer;
    padding: 14px 16px;
    transition: 0.3s;
    font-size: 17px;
}

/* Change background color of buttons on hover */
.tab button:hover {
    background-color: #ddd;
}

/* Create an active/current tablink class */
.tab button.active {
    background-color: #ccc;
}

/* Style the tab content */
.tabcontent {
    display: none;
    padding: 6px 12px;
    border: 1px solid #ccc;
    border-top: none;
}
</style>
</head>
<body>

<div class="tab">
     <button class="tablinks" id='0' onclick="openCity(event, 'Sales')">Sales</button> 

      <button class="tablinks" id='1' onclick="openCity(event, 'Costs')">Costs</button>
            <button class="tablinks" id='2' onclick="openCity(event, 'Expenses')">Expenses</button>
        <button class="tablinks" id='3' onclick="openCity(event, 'Inventory')">Inventory</button>
  <button class="tablinks" id='4' onclick="openCity(event, 'Products')">Products</button>
    <button class="tablinks" id='5' onclick="openCity(event, 'Customers')">Customers</button> 
   <button class="tablinks" id='6' onclick="openCity(event, 'Performance Reports')">Business Insights</button>
      <button class="tablinks" id='7' onclick="openCity(event, 'Administration')">Administration</button>

  </div>


<div id="Products" class="tabcontent">    
  <h3>Products</h3>




      </div>
<div id="Sales" class="tabcontent">    
  <h3>Sales</h3>



      <div class="tab">
  <button class="tablinks" onclick="openCity(event, 'SaleDetail')">Sale Detail</button>
  <button class="tablinks" onclick="openCity(event, 'SaleSummary')">Sale Summary</button>
  <button class="tablinks" onclick="openCity(event, 'SaleHistory')">Sale History</button>
</div>       
</div>

<div id="SaleDetail" class="tabcontent">
 test     
</div>

<div id="SaleSummary" class="tabcontent">

test2       
</div>
<div id="SaleHistory" class="tabcontent">
Test3
</div>



       <br>


<div id="Customers" class="tabcontent">
  <h3>Customers</h3>




       </div>

<div id="Costs" class="tabcontent">
  <h3>Costs</h3>



</div>
<div id="Expenses" class="tabcontent">
  <h3>Expenses</h3>


</div>
<div id="Inventory" class="tabcontent">
  <h3>Inventory</h3>




</div>
<div id="PerformanceReports" class="tabcontent">
  <h3>Performance Reports</h3>
Analyze business performance
</div>



       <div id="Administration" class="tabcontent">
  <h3>Administration</h3>




</div>

<script>
function openCity(evt, cityName) {
    var i, tabcontent, tablinks;
    tabcontent = document.getElementsByClassName("tabcontent");
    for (i = 0; i < tabcontent.length; i++) {
        tabcontent[i].style.display = "none";
    }
    tablinks = document.getElementsByClassName("tablinks");
    for (i = 0; i < tablinks.length; i++) {
        tablinks[i].className = tablinks[i].className.replace(" active", "");
    }
    document.getElementById(cityName).style.display = "block";
    evt.currentTarget.className += " active";
}
</script>

</body>
</html> 

Upvotes: 1

Related Questions