W3.CSS Navigation Tabs - Show 2 id

334 Views Asked by At

I am using W3.CSS Navigation Tabs but I have problems. I have 2 id, I want to show that 2 id when people, for example, click Tab "London" then show 2 id. In W3.CSS Navigation Tabs code, I can show only 1 id at the same time but I don't how to add 1 more id to show up

W3.CSS Navigation Tabs code

<!DOCTYPE html>
<html>
<title>W3.CSS</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<body>

<div class="w3-container">
  <h2>Tabs in a Grid</h2>

  <div class="w3-row">
    <a href="javascript:void(0)" onclick="openCity(event, 'London');">
      <div class="w3-third tablink w3-bottombar w3-hover-light-grey w3-padding">London</div>
    </a>
    <a href="javascript:void(0)" onclick="openCity(event, 'Paris');">
      <div class="w3-third tablink w3-bottombar w3-hover-light-grey w3-padding">Paris</div>
    </a>
    <a href="javascript:void(0)" onclick="openCity(event, 'Tokyo');">
      <div class="w3-third tablink w3-bottombar w3-hover-light-grey w3-padding">Tokyo</div>
    </a>
  </div>

  <div id="London" class="w3-container city" style="display:none">
    <h2>London</h2>
    <p>London is the capital city of England.</p>
  </div>

  <div id="Paris" class="w3-container city" style="display:none">
    <h2>Paris</h2>
    <p>Paris is the capital of France.</p> 
  </div>

  <div id="Tokyo" class="w3-container city" style="display:none">
    <h2>Tokyo</h2>
    <p>Tokyo is the capital of Japan.</p>
  </div>
</div>

<script>
function openCity(evt, cityName) {
  var i, x, tablinks;
  x = document.getElementsByClassName("city");
  for (i = 0; i < x.length; i++) {
    x[i].style.display = "none";
  }
  tablinks = document.getElementsByClassName("tablink");
  for (i = 0; i < x.length; i++) {
    tablinks[i].className = tablinks[i].className.replace(" w3-border-red", "");
  }
  document.getElementById(cityName).style.display = "block";
  evt.currentTarget.firstElementChild.className += " w3-border-red";
}
</script>

</body>
</html>
1

There are 1 best solutions below

7
On BEST ANSWER

A simple solution would be passing multiple city names as an array instead of single city name to the openCity function.

In the following example, the content of 2 tabs with the id London and Paris will show up when you click on the tab London.

function openCity(evt, cityNames) {
    let i, x, tablinks;
    
    x = document.getElementsByClassName("city");
    for (i = 0; i < x.length; i++) {
        x[i].style.display = "none";
    }
    
    tablinks = document.getElementsByClassName("tablink");
    for (i = 0; i < x.length; i++) {
        tablinks[i].className = tablinks[i].className.replace(" w3-border-red", "");
    }
  
    cityNames.map(function(city) {
        document.getElementById(city).style.display = "block";
    });
    
    evt.currentTarget.firstElementChild.className += " w3-border-red";
}
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">

<div class="w3-container">
    <h2>Tabs in a Grid</h2>

    <div class="w3-row">
        <a href="javascript:void(0)" onclick="openCity(event, ['London','Paris']);">
            <div class="w3-third tablink w3-bottombar w3-hover-light-grey w3-padding w3-border-red">London</div>
        </a>
        <a href="javascript:void(0)" onclick="openCity(event, ['Paris']);">
            <div class="w3-third tablink w3-bottombar w3-hover-light-grey w3-padding">Paris</div>
        </a>
        <a href="javascript:void(0)" onclick="openCity(event, ['Tokyo']);">
            <div class="w3-third tablink w3-bottombar w3-hover-light-grey w3-padding">Tokyo</div>
        </a>
    </div>

    <div id="London" class="w3-container city">
        <h2>London</h2>
        <p>London is the capital city of England.</p>
    </div>

    <div id="Paris" class="w3-container city" style="display:none">
        <h2>Paris</h2>
        <p>Paris is the capital of France.</p> 
    </div>

    <div id="Tokyo" class="w3-container city" style="display:none">
        <h2>Tokyo</h2>
        <p>Tokyo is the capital of Japan.</p>
    </div>
</div>