What is the best way to link an anchor tag to a specific tab's content section

*Attempting to understand how to implement an anchor tag that will open a specific tab*

*JavaScript code for navigation tabs*

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";
}
*CSS styling for the page and navigation tabs*

body {
  font-family: Arial;
}


/* Style the tabs */

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


/* Style the buttons inside the tabs */

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


 
*This refers to the HTML section*
**When clicking the link "Tokyo," it should open up the Tokyo tab. Normal Href linking does not seem to work in this case.**

   

 <h2>Tabs</h2>
    <p>Click on the buttons inside the tabbed menu:</p>

    <a href="#Tokyo">goes to Tokyo tab content</a>

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

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

   <!-- More content goes here -->

Answer №1

function toggleCity(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: Verdana;
}


/* Styling the tabs */

.tab {
  overflow: hidden;
  border: 1px solid #999;
  background-color: #e5e5e5;
}
// Tab button style

.tab button {
  background-color: transparent;
  float: left;
  border: none;
  outline: none;
  cursor: pointer;
  padding: 12px 14px;
  transition: 0.2s;
  font-size: 15px;
}

// Hover effect on the buttons inside the tab
.tab button:hover {
  background-color: #ccc;
}

// Active/current tablink styling
.tab button.active {
  background-color: #999;
}

// Content of the tab styling
.tabcontent {
  display: none;
  padding: 5px 10px;
  border: 1px solid #999;
  border-top: none;
}
 <h3>Tabs</h3>
    <p>Click to switch between the different cities:</p>

    <a href="javascript:document.querySelector('#tb').click()">Tokyo Tab/Navigate Tokyo City</a>

    <div class="tab">
      <button class="tablinks" onclick="toggleCity(event, 'London')">London</button>
      <button class="tablinks" onclick="toggleCity(event, 'Paris')">Paris</button>
      <button class="tablinks" id="tb" onclick="toggleCity(event, 'Tokyo')">Tokyo</button>
    </div>

    <div id="London" class="tabcontent">
      <h4>London</h4>
      <p>London is the capital city of England.</p>
    </div>

    <div id="Paris" class="tabcontent">
      <h4>Paris</h4>
      <p>Paris serves as the capital of France.</p>
    </div>

    <div id="Tokyo" class="tabcontent">
      <h4>Tokyo</h4>
      <p>Tokyo, where traditional meets modern.</p>
      <p>Tokyo also offers unique experiences with a blend of tradition and technology! A metropolis that never sleeps and always has something new to discover!</p>
      <p>Tokyo is an eternally innovative and dynamic city. The lively Japanese capital will keep you entertained with its endless opportunities. From historic temples to bustling neon-lit streets, Tokyo has it all!</p>
      <p id="test">Tokyo - vibrant, diverse, and full of energy. Discover what makes this city incredible through its rich culture, food, and people!</p>
      <p>Experience the beauty of Tokyo through its traditions, cuisine, and lifestyle. This eternal city is bound to leave a lasting impression on you!</p>
      <p>Explore the wonder that is Tokyo, from serene gardens to innovative tech hubs! Every corner holds a unique surprise waiting just for you!</p>
      <p>Tokyo - a city like no other. Immerse yourself in its charm and discover why it stands out as one of the world's most fascinating destinations!</p>
    </div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Customize your Bootstrap navbar dropdown to showcase menu items in a horizontal layout

I have been developing a new project that requires a navbar to be positioned at the center of the page, at the top. The current HTML code I am using is as follows... <div class="navbar navbar-inverse navbar-fixed-top center"> <div class="con ...

JavaScript function for automatic scrolling to the bottom of the page is not functioning as expected

I'm working on incorporating a terminal/console feature into my website. I came across the JavaScript functions for scrolling down a page, namely window.scrollTo(0,document.body.scrollHeight); and window.scrollTo(0,document.querySelector(".fakeSc ...

What is the process for connecting controls to Canvas sprites?

Any input is EXTREMELY helpful! To put it shortly, I'm in need of assistance with utilizing HTML5/CSS3 buttons to manage sprite animations on my canvas. These buttons are responsible for controlling the direction and speed of the sprites independentl ...

Ruby Thin Server: An Unexpected ActionView Template Error

Encountering an issue when trying to deploy my project on Amazon Web Services and accessing the domain. ActionView::Template::Error (Command 'java -jar /home/ubuntu/.rvm/gems/ruby-2.1.10@silk/gems/yui-compressor-0.12.0/lib/yui/../yuicompressor-2.4.8 ...

The rendering of React child components is not functioning

Hi there! I am new to using React and I am currently facing an issue with rendering components. My problem is that only the parent component is being displayed, and the child components are not being recognized by React. Here is the snippet of code from m ...

Exploring the distinctions between the Decorator and Mediator design patterns when implemented in JavaScript

After delving into JavaScript patterns, I noticed some interesting differences between Angular 4 and Angular 1.x. Angular 4 employs new patterns that caught my attention. What specific patterns does Angular 4 utilize? Is it possible to incorporate the De ...

What is the best way to position my logo on top of the stunning space background?

Click here to check out the code on CodePen Please take a look at my code on codepen.io. I am new to Stack Overflow so please be patient with me if I make any mistakes. ;-; I currently have my logo (https://i.stack.imgur.com/W0nWc.png) included in the co ...

Interactive JavaScript button that navigates me to a document without the need to click

I'm facing an issue with my small project. I've been learning javascript and managed to create a script that calculates the square of a number provided by the user. var checkIt = function(){ var theNumber = Number(prompt("Please enter a number ...

Creating a carousel in AngularJS using ng-repeat without resorting to jQuery

Looking for a solution to display elements on my page with ng-repeat in a carousel-like fashion. Each element should have a photo, short description, and reveal a long description on click. It's important that it's mobile-friendly so users can sw ...

Could anyone assist me in resolving the error stating, "The 'Argument `where` of type UserWhereUniqueInput needs at least one of `id` arguments"?

Upon investigating, I inserted this console log to determine what data is being received: { username: 'aa', gender: 'Feminino', cargo: '2', email: 'a', password: 'a' } Lately, I've been facing this err ...

Troubleshoot: Json causing issue with displaying markers on Google Maps API (v3)

I developed a custom Google Maps application using JSON data and implemented PostgreSQL database integration. Here is the code snippet: <script type="text/javascript"> var map; var national = [{"lng":"-6.173319","city":"JAKARTA","lat":"106.818 ...

Starting object arrays in Angular 6 using ES6

As someone who is just starting out with javascript, I have encountered a challenge with a nested class structure. Specifically, I am looking to initialize an array of EventDate objects and assign it to 'this.dates' within the CustomerEvents cons ...

Encountering a problem with displaying error messages in an empty text field

I am facing an issue with displaying error messages when a text field is left blank. I would like a simple message, such as "can't be empty", to appear below the text field in red color when the user clicks the submit button and leaves multiple fields ...

Bootstrap for arranging DIVs in a responsive manner

I am trying to place some DIV elements inside their parent container, whether it is the body, a table's cell, or another div, using Bootstrap 5. The desired layout is shown on the attached illustration: https://i.sstatic.net/QKwjK.png If there is an ...

Recording incoming audio from headphones programmatically involves accessing the audio input stream, which can be done by

I need assistance in creating a JavaScript application that can record incoming audio through headphones. I am able to record my own voice, but I am struggling to capture the audio of the other person speaking through the same headphones. ...

Was not able to capture the reaction from the Angular file upload

I've been attempting to upload a single file using the POST Method and REST Calling to the server. Despite successfully uploading the module with the code below, I encounter an error afterwards. Is there anyone who can lend assistance in troubleshooti ...

Keeping track of important dates is ineffective using the calendar

I am facing an issue with developing a calendar that marks events on the correct dates. I am receiving the dates in the following format in PHP [ { "status": "OK", "statusCode": 200, "statusMensagem": & ...

The functionality of the typeof operator is not behaving as anticipated

I am currently working on a script to verify the existence of a specific JavaScript object. var success = function(data) { var x= 0; var numOfCards = data.length; for (x=0;x<data.length - 1;x++) { if (typeof data[x].l ...

Converting Three.js objects into arrays

I am new to Three.js and I want to add my 'palmtree' object to an array. I believe my code is correct, but something seems off. Here is the snippet of my code: var objects = []; var mtlLoader = new THREE.MTLLoader(); mtlLoader.load("objects/pal ...

Steps to display a single entry in a Laravel/Vue implementation

My goal is to combine Laravel with Vue and eventually Nuxt in order to incorporate dynamic page transitions similar to the ones showcased on into my websites. I recently followed a tutorial on using Vue with Laravel at https://scotch.io/tutorials/build-a ...