Having issues with the functionality of vertical tabs in Javascript

I have set up vertical tabs, but they seem to be duplicating all over the screen. My suspicion is on bootstrap, although I cannot confirm it.

https://www.w3schools.com/howto/howto_js_vertical_tabs.asp I obtained the code for the vertical tabs from this source

Here is a snapshot of the issue: https://i.sstatic.net/5EJke.jpg (The image link only works this way) I attempted modifying some bootstrap elements without any success.

<div style="padding-top:100px;padding-bottom:100px" class="container">
    <div class="row">
        <div class="col-md-3 ">
             <div class="list-group">
              <a id="defaultOpen" style="padding: .75rem 1.25rem;" href="openCity(event, 'Dashb')" class="tablinks list-group-item list-group-item-action btn">Account Dashboard</a>
              <a style="padding: .75rem 1.25rem;" href="openCity(event, 'Info')" class="tablinks list-group-item list-group-item-action btn">Account Information</a>
              <a style="padding: .75rem 1.25rem;" href="openCity(event, 'Adbook')" class="tablinks list-group-item list-group-item-action btn">Address Book</a>
              <a style="padding: .75rem 1.25rem;" href="openCity(event, 'Orders')" class="tablinks list-group-item list-group-item-action btn">My Orders</a>
              <a style="padding: .75rem 1.25rem;" href="openCity(event, 'Preview')" class="tablinks list-group-item list-group-item-action btn">My Product Reviews</a>
              <a style="padding: .75rem 1.25rem;" href="openCity(event, 'Newsl')" class="tablinks list-group-item list-group-item-action btn">NewsLetter</a>
              <a style="padding: .75rem 1.25rem;" href="openCity(event, 'Psub')" class="tablinks list-group-item list-group-item-action btn">My Product Subscribtions</a>
              <a style="padding: .75rem 1.25rem;" href="openCity(event, 'Creditc')" class="tablinks list-group-item list-group-item-action btn">My Credit Cards</a>
            </div> 
        </div>

<div class="col-md-9">
 <div id="Dashb" class="card tabcontent">
<div class="card-body">
 <div class="row">
 <div class="col-md-12">
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-12">
                            <form>
                              <div class="form-group row">
                                <label for="username" class="col-4 col-form-label">User Name</label> 
                                <div class="col-8">
                                  <input id="username" name="username" placeholder="Username" class="form-control here" required="required" type="text">
                                </div>
                              </div>
                              <div class="form-group row">
                                <label for="name" class="col-4 col-form-label">First Name</label> 
                                <div class="col-8">
                                  <input id="name" name="name" placeholder="First Name" class="form-control here" type="text">
                                </div>
                              </div>
                              <div class="form-group row">
                                <label for="lastname" class="col-4 col-form-label">Last Name</label> 
                                <div class="col-8">
                                  <input id="lastname" name="lastname" placeholder="Last Name" class="form-control here" type="text">
                                </div>
                              </div>

function openCity(evt, cityName) {
  // Declare all variables
  var i, tabcontent, tablinks;

  // Get all elements with class="tabcontent" and hide them
  tabcontent = document.getElementsByClassName("tabcontent");
  for (i = 0; i < tabcontent.length; i++) {
    tabcontent[i].style.display = "none";
  }

  // Get all elements with class="tablinks" and remove the class "active"
  tablinks = document.getElementsByClassName("tablinks");
  for (i = 0; i < tablinks.length; i++) {
    tablinks[i].className = tablinks[i].className.replace(" active", "");
  }

  // Show the current tab, and add an "active" class to the link that opened the tab
  document.getElementById(cityName).style.display = "block";
  evt.currentTarget.className += " active";
}

Answer №1

To properly hide the tabcontent at the beginning and only display the first tab, a piece of JavaScript code is required. This code can be found on the W3C 'try it out' page:

 document.getElementById("defaultOpen").click();

This line of code should be placed at the end of your JavaScript code (outside of any functions).

The instructions provided in the W3C article may not explicitly mention this step. Without implementing this code, all tabcontent blocks will initially appear visible.

Alternatively, you could use CSS to hide the tabcontent blocks by default and then reveal the first one. Although the JS solution is more straightforward, you may notice a slight flicker upon page load before the tabs are hidden.

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

jquery carousel slider dynamically append

I have been tasked with creating a dynamic carousel using jQuery AJAX, and my supervisor recommended that I utilize the .append() method. Below is the HTML code for my carousel: <div id="myCarousel"> </div> <div cla ...

Steps for accessing a desktop folder using ElectronJS

Need assistance with a task involving opening data from the back-end in electron and displaying it as a desktop folder. https://i.sstatic.net/tCoHF.png Feeling a bit lost on how to accomplish this. For instance, I receive data like {id:1, foldername: &a ...

Tag used when importing XML file in HTML

I'm struggling to come up with the best question to ask about this. When it comes to importing CSS and JS, you will need to use: <link rel="stylesheet" type="text/css" href="bootstrap.min.css"/> <script type="text/javascript" src="jquery ...

Incorrect placement of navigation in HTML/CSS dimensions

I'm working on my new portfolio and I've run into an issue with the navigation placement that I can't seem to solve. The text should be aligned with the lines on the sides, but instead it's shifted right and down. I'm struggling t ...

Searching for variables within files using Node.js and constructing an object from the results

Trying to figure out how to streamline this process. Here's the directory structure I'm working with: src/ modules/ clients/ i18n/ en-US.ts tasks/ i18n/ en-US.ts So, ea ...

Struggling to retrieve a response from the ListUsers endpoint in OKTA using the okta-sdk-nodejs Client's listUsers function

Code snippet: async fetchUsersByEmail(email) { try { return await Promise.all([ oktaClient.listUsers({ search: email, }), ]).then((response) => { console.log(response); }); } catch (error) { ...

Creating a personalized lightbox that automatically plays with animations

Can anyone help me create a personalized lightbox that autoplays with a simple animation? I'm wondering if it's achievable using just jQuery code or if I need to incorporate external plugins. Any guidance would be greatly appreciated. Thank you! ...

The Bootstrap carousel slider is functioning properly, however, the images are not displaying

I am experiencing an issue with my carousel. I have been developing a project and utilizing the bootstrap-4 carousel slider. It functions properly when I run it on my localhost, displaying all the images as expected. However, after publishing the project o ...

Can you help me make a JavaScript Random Number Generator that utilizes HTML input fields and buttons?

I am currently working on developing a random number generator that takes user input through HTML. The idea is to have the user enter two values and then click "submit" to receive a random number within that range. However, I seem to be stuck at this poin ...

How can I implement disabling buttons for specific IDs in React?

I'm currently developing an interactive quiz application with React that features multiple choice questions. I've implemented state management to track and increment the user's score when they select the correct answer option, but there&apos ...

retrieve JSON object from deferred response of AJAX request

After utilizing Ajax to send an item to a SharePoint list, I aim to incorporate the jsonObject received in response into a list of items. Located in AppController.js $scope.addListItem = function(listItem){ $.when(SharePointJSOMService.addListIte ...

Why isn't the parent div stretching alongside its child divs?

Take a look at this example: wthdesign.net/website/olaf&co/index.php I am currently working on creating a responsive layout, but I am struggling to figure out how to make the "#content" div stretch with its child div when there is more content than ex ...

Capturing Data from Tables and Saving it with Protractor

Imagine having a table structured like this <h2>HTML Table</h2> <table> <tr> <th>Company</th> <th>Contact</th> <th>Code</th> </tr> <tr> <td>Alfreds Fu ...

Manipulating datetime format within an input element of type date using Angular's ngModel

I have a date input in my form that is populated from the controller with a string value for 'dateOfDiagnosis'. The format of this string includes the time as well, like this: "2010-09-08T00:00:00" To bind this value to an input field in Angu ...

What are the steps to fixing the date time issue between NextJS and Firebase?

I am facing an issue with Firebase Database returning timestamps and unable to render them into components using Redux. How can I resolve this error and convert the timestamp to a date or vice versa? I need help with valid type conversion methods. import ...

I'm having trouble getting an audio file to play in my Laravel application. I uploaded the audio file to local storage, but when I try to play it using the audio tag, it's not

Having trouble with the path of my mp3 file stored in local storage and saved in the database. When retrieving the record from the database along with the mp3 file, it seems like the path is incorrect and I can't play it. I executed php artisan stora ...

How to reposition a DIV element using CSS only

I'm facing a challenge where I need to relocate a DIV element to the bottom of the body without altering the HTML structure. I tried referring to some existing threads for solutions, like this one on Stack Overflow: Moving a div from bottom to top, us ...

Compiling Enum classes from Typescript to JavaScript leads to errors in export

I'm currently facing an issue with exporting Kotlin Enum classes to JS. @OptIn(ExperimentalJsExport::class) @JsExport enum class interEnum { SAMPLE } When I import the enum into an Angular Project as an NPM module, the corresponding TS block in m ...

Angular Selectable Drop-down Menu

I'm facing an issue with using angularjs for dropdown with multiple selection. When I try to include 2 dropdowns in the same form, only one of them gets initialized properly. Here is a sample code snippet http://jsfiddle.net/vUSPu/1221/. Can someone p ...

The clarity and completeness of images on canvas are lacking

I've experimented with numerous examples found on the internet, but none of them seem to be effective. My goal is to display a full image on a canvas without it being zoomed in and losing quality. In addition, I attempted to rotate images (from stand ...