Trouble with tab switching across multiple cards

I have been working on an app that generates multiple cards with 3 tabs on each card. However, I am facing an issue with tab switching. The tab switching works fine on the first card, but when I try to switch tabs on other cards, it still affects the tabs on the first card only. I am having trouble understanding why this is happening.

$(document).on('click', '.navigation a', function(e) {
  e.preventDefault();
  $(this).tab('show');
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="aedec1dedecbdc80c4ddee9f809f98809f">[email protected]</a>/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>

<div class="card-columns">
  <div class="card">
    <div class="card-header">
      <ul class="nav nav-tabs card-header-tabs navigation" role="tablist">
        <li class="nav-item">
          <a class="nav-link active" href=".info" role="tab" aria-controls="info" aria-selected="true" data-toggle="tab">Info</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href=".ingredients" role="tab" aria-controls="ingredients" aria-selected="false" data-toggle="tab">Ingredients</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href=".nutrition" role="tab" aria-controls="nutrition" aria-selected="false" data-toggle="tab">Nutrition information</a>
        </li>
      </ul>
    </div>

    <div class="card-body">
      <h4 class="card-title">Item name</h4>
      <div class="tab-content mt-3">
        <div class="tab-pane active info" role="tabpanel">
          <img src="https://images.pexels.com/photos/1279330/pexels-photo-1279330.jpeg?auto=compress&cs=tinysrgb&dpr=3&h=750&w=1260" class="card-img-top" alt="">
          <div class="text-center">
            <a href="" target=_blank><button type="button" class="btn btn-primary">Full recipe</button></a>
            <a href=""><button type="button" class="btn btn-primary">Add to favorites</button></a>
          </div>
        </div>

        <div class="tab-pane ingredients" role="tabpanel" aria-labelledby="ingredients-tab">
          <h6 class="card-subtitle mb-2">Ingredient list</h6>
          <ul id=ingredientList>
            <li>Ingredient</li>
            <li>Ingredient</li>
            <li>Ingredient</li>
            <li>Ingredient</li>
          </ul>
        </div>

        <div class="tab-pane nutrition" role="tabpanel" aria-labelledby="nutrition-tab">
          <h6 class="card-subtitle mb-2">Nutrition information</h6>
          <ul id=nutritionInfo>
            <li>Nutrition item</li>
            <li>Nutrition item</li>
            <li>Nutrition item</li>
            <li>Nutrition item</li>
            <li>Nutrition item</li>
            <li>Nutrition item</li>
          </ul>
        </div>
      </div>
    </div>
  </div>

  </div>

Example of the Issue on Codepen

Answer №1

It is due to the combination of classes and href attributes being used. Here is an alternative example for you to try:

$(document).on('click','.navigation a', function (e) {
    e.preventDefault();
    $(this).tab('show');
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet"/>
<div class="card-columns">
  <div class="card">
    <div class="card-header">
      <ul class="nav nav-tabs card-header-tabs navigation" role="tablist">
        <li class="nav-item">
          <a class="nav-link active" href=".info" role="tab" aria-controls="info" aria-selected="true" data-toggle="tab">Info</a>
        </li>
        <li class="nav-item">
          <a class="nav-link"  href=".ingredients" role="tab" aria-controls="ingredients" aria-selected="false" data-toggle="tab">Ingredients</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href=".nutrition" role="tab" aria-controls="nutrition" aria-selected="false" data-toggle="tab">Nutrition information</a>
        </li>
      </ul>
    </div>
    
    <div class="card-body">
      <h4 class="card-title">Item name</h4>
      <div class="tab-content mt-3">
        <div class="tab-pane active info" role="tabpanel">
          <img src="https://images.pexels.com/photos/1279330/pexels-photo-1279330.jpeg?auto=compress&cs=tinysrgb&dpr=3&h=750&w=1260" class="card-img-top" alt="">
          <div class="text-center">
            <a href="" target=_blank><button type="button" class="btn btn-primary">Full recipe</button></a>
            <a href=""><button type="button" class="btn btn-primary">Add to favorites</button></a>
          </div>
        </div>

        <div class="tab-pane ingredients" role="tabpanel" aria-labelledby="ingredients-tab">
          <h6 class="card-subtitle mb-2">Ingredient list</h6>
          <ul id=ingredientList>
            <li>Ingredient</li>
            <li>Ingredient</li>
            <li>Ingredient</li>
            <li>Ingredient</li>
          </ul>
        </div>

        <div class="tab-pane nutrition" role="tabpanel" aria-labelledby="nutrition-tab">
          <h6 class="card-subtitle mb-2">Nutrition information</h6>
          <ul id=nutritionInfo>
            <li>Nutrition item</li>
            <li>Nutrition item</li>
            <li>Nutrition item</li>
            <li>Nutrition item</li>
            <li>Nutrition item</li>
            <li>Nutrition item</li>
          </ul>
        </div>
      </div>
    </div>
  </div>
  </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

What could be causing the malfunction of the dynamic Bootstrap version 3 date picker?

Just starting out with Twitter Bootstrap version 3 and I found an example at this link. I want to add multiple date ranges dynamically, like repeating the process 'n' number of times. Everything works fine when done manually. However, when try ...

Error encountered while pressing key on vscode led to failure in requesting textDocument/documentLink

Greetings! I'm currently using VSCode on both my mac and Windows 10 machines. Recently, they have both started experiencing the same issue after a recent update/rollback. Every few keystrokes, the output box pops up with the "HTML Language Server" se ...

The Struggle with Bootstrap Accordion

I'm currently in the process of learning bootstrap. I copied the code for the 'Flush' Accordion from the bootstrap docs, but my version looks completely off. Despite linking the CSS stylesheet and JS Bundle, it doesn't seem to align pro ...

Discover the identity of an element through the value of another element

Looking for a way to retrieve the id value based on the input value in this HTML code. For instance, I need to identify the id value associated with the input value of number2 - which is unknown2. How can this be achieved using jQuery? <th id="unknow ...

Open the .exe file using an HTML link in Firefox browser

While working on a project using php / js / html, I encountered the need to launch a C# application from my web page without requiring any downloads. The challenge was that I primarily use Mozilla Firefox and could only find solutions involving ActiveXOb ...

Setting the opacity of an image will also make the absolute div transparent

I currently have an image gallery set up with the following structure : <div class="gallery"> <div class="message"> Welcome to the gallery </div> <img src="http://placehold.it/300x300" /> </div> Here ...

Selection pseudo selectors for tooltips are a great way to provide additional

Can someone explain how tooltips change on Medium.com when you double click a word or sentence to reveal options like share and edit? https://i.stack.imgur.com/0EVmH.png ...

Finding the Attachment ID on a JIRA Issue Page with JavaScript

Currently, I am using an ajax call that requires the attachment id in its URL. The URL is hardcoded as follows: url: AJS.contextPath()+"/rest/api/latest/attachment/10415" jQuery.ajax({ url: AJS.contextPath()+"/rest/api/latest/attachment/10415", TYPE: "GET ...

Storing JSON results in cache using Twitter Bootstrap Typeahead

Using the twitter bootstrap library in my application has been a fantastic experience. I am currently working on implementing bootstrap typeahead for autocomplete and facing an issue with caching the results to reduce server requests. During my research, I ...

What is the best way to retrieve the value of a submitted button in a jQuery form submission?

I am facing an issue with my HTML code. Here is the structure: <form method="post" id="IssueForm" action="wh_sir_insert.php"> <input name='id[]' type='checkbox' class="selectable" value='$col[8]' /> <inpu ...

How to extract a value from a span input textbox using Vue?

I just started using Vue and I'm attempting to create an auto-growing input. I've realized that it's not possible to create a real <input> element that adjusts its size based on its content and allows for value modifications using v-mo ...

Troubleshooting: Bootstrap 5 Alert not Displaying on Website

I'm experiencing an issue trying to display a bootstrap alert in my HTML code. Here is the code I'm using: <div class="alert alert-success alert-dismissible fade show" role="alert"> text & ...

After updating to Angular 7, an error was encountered: "TypeError: Unable to execute map function on ctorParameters"

After updating my Angular project to version 7, I encountered a new issue. When running "ng serve --open" from the CLI, I received the following error message: Uncaught TypeError: ctorParameters.map is not a function at ReflectionCapabilities._own ...

Tips for building a custom error handling function specifically designed for parsing and handling errors in

Is there a way to reduce redundancy in my code, such as: let name = ""; try { name = data[" "][0][" "][0][" "][0][" "][1][" "][0][" "][1]["C"]; } catch (error) { if (error instanceof TypeError) { console.log("name TypeError"); } } I have consid ...

Exploring the endless possibilities of using repeatable bootstrap panels

The current code structure is as follows (custom wells, labeled as well-xxxx, apply brand colors to default wells): <div class="panel-group" id="accordion1"> <div class="panel panel-info"> <div class="panel-heading" data-toggle="colla ...

Display a div when hovering over a span with custom styling

Don't let the title fool you. http://jsfiddle.net/whb8A/ I'm struggling to style the text inside a span tag within a div. The h3 element shouldn't be nested in the span, but removing it makes it difficult to style with CSS. When hovering ...

Utilizing jQuery and DOM to interact with form elements

Below is the form content <form method="post"> <input type="hidden" name="resulttype" value="Create"> <table> <tr> <td>Full Name</td> <td><input ...

jQuery validation rules and attributes

Is it possible to apply custom validation to a dynamically generated control using the .attr() method for setting rules? $.fn.addValidationExpression = function(field) { if (field.ValidationExpression != null) { $("#fld"+getFieldIdSuffix(fie ...

I am facing unexpected results while trying to search for a string array object using elemMatch in Mongoose

I am encountering an issue that I can't seem to resolve. I need to utilize a query search to match the data of one job with user data, but I'm facing some obstacles. The primary challenge is within my query search for the job where the data looks ...

What is the best way to retrieve route data based on route name in VueJs?

When working with VueJs, I have found that I can easily access the data of the current route. However, I have encountered a situation where I need to access the data of a different route by its name. Let's say I have defined the following routes: [ ...