G.L.P
0
Q:

javascript tabs example


  <!-- Tab links -->
<div class="tab">
  <button class="tablinks" onclick="openCity(event, 
  'London')">London</button>
  <button class="tablinks" onclick="openCity(event, 
  'Paris')">Paris</button>
  <button class="tablinks" onclick="openCity(event, 
  'Tokyo')">Tokyo</button>

</div>

<!-- Tab content -->
<div id="London" class="tabcontent">
  <h3>London</h3>
  
<p>London is the capital city of England.</p>
</div>

<div 
id="Paris" class="tabcontent">
  <h3>Paris</h3>
  <p>Paris 
is the capital of France.</p> 
</div>

<div id="Tokyo" class="tabcontent">
  
<h3>Tokyo</h3>
  <p>Tokyo is the capital of Japan.</p>
</div> 
2
const showHideAll =(list)=>{
  for(var i = 0; i < list.length; i++){
      list[i].classList.remove('show');
      list[i].classList.add('hide'); // depending on what you're doing
  }
}

window.onload = function() {
  let innerDiv = document.getElementsByClassName("inner-description");
  let clicked = document.querySelectorAll('.show-hide-description');
  clicked.forEach((item, i) => {
    let clickedId = item.id;
    document.querySelector(`#${clickedId}`).addEventListener('click',()=>{
      let clickedIdDescription = document.getElementById(`${clickedId}-description`);
      if(clickedIdDescription.classList.contains('show')){
        showHideAll(innerDiv)
      } else {
        showHideAll(innerDiv)
        clickedIdDescription.classList.remove('hide');
        clickedIdDescription.classList.add('show');
      }
    })
  });
}
1

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

  
/* Style the buttons that are used to open the tab content */
.tab button {

    
  background-color: inherit;
  float: left;
  
  border: none;
  outline: none;
  cursor: pointer;
  padding: 14px 16px;
  transition: 0.3s;
}

/* 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;
} 
1

New to Communities?

Join the community