Clicking on tabs causes their content to mysteriously vanish

I am working on creating a dynamic menu using jQuery that switches content based on tabs. However, I am facing an issue where clicking on a different <li> item causes the content to disappear. Each list item should correspond to different content sections displayed below.

Apologies for the missing images in the menu items, but I hope you can still grasp the concept.

The initial content is visible, but as soon as I try to switch it, everything disappears.

$(document).ready(function() {

  $('ul li').click(function() {
    var tab_id = $(this).attr('data-tab');

    $('ul li').removeClass('active');
    $('.content').removeClass('current');

    $(this).addClass('active');
    $("#" + tab_id).addClass('current');
  });

});
/* ASIDE */


/* NAV */

aside {
  float: left;
  width: 30%;
}

aside nav {
  text-align: center;
  justify-content: center;
}

aside nav ul {
  display: inline-block;
  list-style: none;
  padding: 0;
}

aside nav ul li {
  display: inline-block;
  margin: 2px 0px;
  text-align: center;
  font-size: 10px;
  padding: 6px 6px 12px 6px;
  color: black;
}

.active {
  text-decoration: none;
  color: black;
  border-bottom: solid 1px black;
}

aside nav ul li img {
  display: block;
  width: 1.1rem;
  height: 1.1rem;
  margin: 4px auto;
}

aside div {
  height: 100vh;
  overflow-x: scroll;
  padding: 12px;
  margin-left: 2px;
}

.content {
  display: none;
}

.content.current {
  display: inline-block;
}

aside .content img {
  width: 100%;
  height: auto;
}

aside .content label {
  font-size: 12px;
}

.content p {
  font-size: 14px;
  text-decoration: underline;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav id="tabs" role="navigation" aria-label="menu">
  <ul class="tabs">
    <li tabindex="1" data-tab="tab-1"><img src="img/figures.svg" alt="figures selector">Figures</li>
    <li tabindex="2" data-tab="tab-2"><img src="img/references.svg" alt="references selector">References</li>
    <li tabindex="3" data-tab="tab-3"><img src="img/related.svg" alt="related selector">Related</li>
    <li tabindex="4" data-tab="tab-4"><img src="img/info.svg" alt="details selector">Details</li>
  </ul>
</nav>

<!-- CONTENT -->
<div id="tab-1" role="content" aria-label="figures content" class="content current">
  <img src="img/figure1.jpg" alt="figure 1" id="fig1" value="fig1">
  <label for="fig1">Fig. 1 Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt</label>
  <hr/>
  <img src="img/figure2.png" alt="figure 2" id="fig2" value="fig2">
  <label for="fig2">Fig. 2 Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt</label>
  <hr/>
  <img src="img/figure3.png" alt="figure 3" id="fig3" value="fig3">
  <label for="fig3">Fig. 3 Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt</label>
  <hr/>
</div>
<div id="tab-2" role="content" aria-label="references content" class="content">
  <h3>Lorem</h3>
  <hr>
  <p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
  <h3>Lorem</h3>
  <hr>
  <p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
</aside>

<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="js/script.js"></script>

Answer №1

If you need to retrieve the data-tab attribute from the li element that was just clicked, use the following code:

var tabID = $(this).attr('data-tab');

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

Is it necessary to adjust my font stack for optimal viewing on a high DPI tablet or computer screen?

My CSS includes the following font definition: html { font-size: 95%; font-family: Arial, Helvetica, sans-serif } I want my application to display well on PC, iOS, and Android tablets. I am aware that different devices have varying resolutions. C ...

Sending form data using Ajax

My concern is quite straightforward - I am using jquery and ajax to submit a form. When the button is clicked, the data gets sent to the database. The question is - if the user clicks the submit button twice, there will be two entries in the database. Ho ...

Establishing a Goal in Google Analytics using JavaScript

My approach to recording Goals in Google Analytic tool via JS involves adding code directly to the page: <meta name="description" content="iReserve"> <link rel="shortcut icon" href="/ico/favicon.ico" /> & ...

Get rid of the spaces in web scraping <tr> tags using Node.js

I've encountered a problem that goes beyond my current knowledge. I'm attempting to web-scrape a specific webpage, targeting the <tr> element in nodejs. Although I can successfully retrieve the content, it seems that the format is not as cl ...

Get the hash value after a specific character using Javascript

I'm currently working on a website with ajax functionality where pages load when clicking on navigation buttons. However, I encounter issues when reloading the page as it defaults back to loading main.html, regardless of the URL. The hash being used i ...

I require fixed headers that no longer stick once reaching a specific point

I have a setup where the names and occupations of participants are displayed next to their artworks, and I've made it sticky so that as we scroll through the images, the names stay on the left. Now, I want the names to scroll out once the images for e ...

Utilizing AJAX to transmit data from an HTML page to a controller in Asp.net

Recently, I started using .net and encountered an issue where I'm attempting to transfer data from a .html file to the controller using AJAX. Here is my ajax call: var dataValue = { ID: 10, Name: 'Test' }; ...

Displaying HTML code in an alert message using an AJAX POST request

When making an AJAX POST request, I faced this issue: $.ajax({ type: 'post', url: 'uploadImages.php', //The current page contentType: "application/json; charset=utf-8", data: { lat: lat, lng: lng }, dataTy ...

Discovering a particular string within a jQuery array object: Tips and tricks

One thing that is confusing me is the jQuery array object. To explain further: I have two arrays, one called brandsLink and the other called floorLink. When a user clicks on a link, I am saving the brand name in a variable called brandName, and then checki ...

What steps should I take to implement AJAX in my filter form?

I am completely new to the world of programming. Right now, I have a list of venues displayed in a table on my index page. Users can filter these venues by type and/or area using checkboxes in a form. Thanks to railscasts, I have successfully implemented ...

Learn how to effectively display checkboxes in an HTML form with Vue configuration

new Vue({ el: '...', data: { checkeditems: [] } }) <div v-for="item in instituteModel" v-if="instituteModel.length > 0"> <input type="checkbox" id="item.id" value="item.inst_name" v-model="checkeditems"/> </div&g ...

Does anyone else have a peculiar problem with css width?

Either I have been designing web pages for an extensive period of time without taking a break or something truly bizarre occurred. <div style="background-color:#0F0; margin:5px; height:5px;"></div> This will generate a lengthy bar with a 5-pi ...

Choose a textarea in a generic manner

Can anyone help me with selecting a textarea from a specific element on my webpage using jQuery? I've been trying different methods without success. Here is an example of what I have attempted: JsFiddle I wanted to use the :input[type='blah&ap ...

The z-index of the fixed header in FullPageJS is causing the section's content to be hidden

For my current project, I am incorporating FullPageJS and using a fixed top menu that adjusts its height based on the viewport size. On smaller screens, the menu items will be stacked on top of each other, while on wider screens, the menu items will be po ...

Unable to execute ajax on dom ready in Internet Explorer 9

Here is some code that needs to be executed on DOM ready without any user interaction: if($.browser.msie){ console.log("Using getJSON"); $.getJSON(baseUrl,function(){ alert('hi'); }); }else{ setTimeou ...

Adding dynamic content to CSS in Next.JS can be achieved by utilizing CSS-in-JS

Currently diving into the world of Next.JS, I've managed to grasp the concept of using getStaticProps to retrieve dynamic data from a headless CMS and integrate it into my JSX templates. However, I'm unsure about how to utilize this dynamic conte ...

Trouble with implementing Ajax in Express and jquery

My app has a browse page where users can add items as owned or wanted. Currently, when adding an item it redirects back to the general /browse page. However, I want the page to not refresh at all. I'm attempting to achieve this with ajax, but as a beg ...

Is anyone else experiencing issues with their imagemap due to Twitter Bootstrap?

I'm excited to ask my first question here as a beginner. I am truly grateful for all the assistance and guidance I have received from this site. I hope that the questions I ask can also benefit others in some way :) Although imagemaps may not be used ...

Where can I locate information on using the .get method?

Recently, I encountered a code snippet on this site that helped me resolve an issue. The individual who provided the code utilized a .GET method that was unfamiliar to me. Here's a sample snippet: If you'd like to see the complete code, you can ...

Trigger a function upon the addition of a class to a div element | Execute AnimatedNumber()

Is there a way to trigger a function when a specific div receives a certain class? I have an animated div that only appears when scrolling, and once it's visible, it gains the class "in-view". I want to execute a function every time this div has the ...