Modal screen for each attribute

I am facing my shortcomings while learning to use JavaScript. I’ve spent hours searching for the answer.

I am trying to create a popup modal screen for each City. But, my implementation of tutorials (1) does not “pop up” modals for each city (2) modal works only on the first <a id=myBtn ...> (3) the working modal shows descriptions of all three cities when I click “London” for the first time (4) and correct city description shows only after clicking the other cities, then clicking “London.”

I want city description to appear in modal screen when I click on the corresponding city name.

If you run the Code Snippet below, you’ll see my shortcomings in action.

var modal = document.getElementById("myModal"); // Get the modal
var btn = document.getElementById("myBtn"); // Get the button that opens the modal
var span = document.getElementsByClassName("close")[0]; // Get the <span> element that closes the modal

// When the user clicks the button, open the modal 
btn.onclick = function() {
  modal.style.display = "block";
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
  if (event.target == modal) {
    modal.style.display = "none";
  }
}
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
  modal.style.display = "none";
}


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";



}
body {font-family: Arial, Helvetica, sans-serif;}

/* The Modal (background) */
.modal {
  display: none; /* Hidden by default */
  position: fixed; /* Stay in place */
  z-index: 1; /* Sit on top */
  padding-top: 100px; /* Location of the box */
  left: 0;
  top: 0;
  width: 100%; /* Full width */
  height: 100%; /* Full height */
  overflow: auto; /* Enable scroll if needed */
  background-color: rgb(0,0,0); /* Fallback color */
  background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}

/* Modal Content */
.modal-content {
  background-color: #fefefe;
  margin: auto;
  padding: 20px;
  border: 1px solid #888;
  width: 80%;
}

/* The Close Button */
.close {
  color: #aaaaaa;
  float: right;
  font-size: 28px;
  font-weight: bold;
}

.close:hover,
.close:focus {
  color: #000;
  text-decoration: none;
  cursor: pointer;
}
<!-- Clickable words -->
<a id="myBtn" class="tablinks" onclick="openCity(event, 'London')">London</a>
<br>
<a id="myBtn" class="tablinks" onclick="openCity(event, 'Paris')">Paris</a>
<br>
<a id="myBtn" class="tablinks" onclick="openCity(event, 'Tokyo')">Tokyo</a>


<!-- Modal Contents -->
<div id="myModal" class="modal">
  <div class="modal-content">
    <span class="close">&times;</span>
    
      <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>
  </div>

</div>

Due credit: I have integrated two examples made by w3schools.

Answer

Theproblem is that You can’t use ID multiple times, it just will not work. To make it work I would just put function to open modal in openCity() like this:

function openCity(evt, cityName) {
  modal.style.display = "block"; //Opening modal after click

  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";
  }

Then the modal will be opened every time you click on any link.