The Bootstrap tab pane does not toggle when the href hash route is being used

Looking to implement a basic hash router for navigating a Bootstrap 4.6 tab component.

Having an issue where calling the .tab('show') function toggles the correct tab but does not toggle its corresponding tab-pane section. Ensured that the tab-panes id attribute matches the tabs href attribute.

I managed to manually show and hide the tab panes, but Bootstrap should handle this automatically. Anyone have a solution?

Thank you!

hashRouteInit = function(callback) {
  window.addEventListener('hashchange', callback, false);
}

const routes = {
  '#/units': function() {
    console.log('units');
  },
  '#/clients': function() {
    console.log('clients');
  },
  '#/searches': function() {
    console.log('searches');
  },

};

hashRouteInit(function() {
  console.log(window.location.hash);
  console.log(routes);
  if (routes[window.location.hash] != undefined) {

    /*how can i remove the following 3 lines and let .tab('show') show the tab pane*/
    /*$(`.tab-content .show`).hide();
    $(`.tab-content .show`).removeClass('show');
    $(`.tab-content .show`).removeClass('active');*/

    $(`#stats-tabs a[href="${window.location.hash}"]`).tab('show');

    /*how can i remove the following 3 lines and let .tab('show') show the tab pane*/
    /*$(`.tab-content .tab-pane[id="${window.location.hash}"]`).addClass('show');
    $(`.tab-content .tab-pane[id="${window.location.hash}"]`).addClass('active');
    $(`.tab-content .tab-pane[id="${window.location.hash}"]`).show();*/

    routes[window.location.hash]();

  }
});
<head>

  <link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/4.6.0/css/bootstrap.min.css" rel="stylesheet">
</head>

<body>
  <div id="container">
    <ul class="nav nav-tabs" id="stats-tabs" role="tablist">
      <li class="nav-item" role="presentation"><a class="nav-link active" id="units-tab" href="#/units" role="tab" aria-controls="units" aria-selected="true">Units</a></li>
      <li class="nav-item" role="presentation"><a class="nav-link" id="clients-tab" href="#/clients" role="tab" aria-controls="clients" aria-selected="false">Clients</a></li>
      <li class="nav-item" role="presentation"><a class="nav-link" id="searches-tab" href="#/searches" role="tab" aria-controls="searches" aria-selected="false">Searches</a></li>
    </ul>

    <div class="tab-content pt-2" id="stats-tabs-content">
      <div class="tab-pane fade show active" id="#/units" role="tabpanel" aria-labelledby="units-tab">units</div>
      <div class="tab-pane fade" id="#/clients" role="tabpanel" aria-labelledby="clients-tab">clients</div>

      <div class="tab-pane fade" id="#/searches" role="tabpanel" aria-labelledby="searches-tab">
        <div id="search-overview-searches">searches</div>
      </div>

    </div>
  </div>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/4.6.0/js/bootstrap.min.js"></script>
</body>

Answer №1

It appears that according to the HTML ID attribute guidelines, the ID must begin with a letter (https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id)

After adjusting the href attributes to #name and using the same value without the number sign for the tab pane ID, it started functioning correctly.

Although the specification (https://www.w3.org/TR/2011/WD-html5-20110525/elements.html#:~:text=The%20id%20attribute%20specifies%20its,not%20contain%20any%20space%20characters.) does not mention anything about avoiding the use of slashes to create a hierarchy in the ID string, attempting to nest another tab section as a child within a tab pane presents challenges when trying to maintain functionality with bootstrap tabs. This limitation leaves only options like - or _ for separation, which may not be as intuitive in a URI format, especially when wanting to include numerical identifiers such as parent/child/3. Unfortunately, this is not allowed, even though the spec does not explicitly prohibit the use of forward slashes in IDs.

Does anyone have insights on why forward slashes are restricted in the ID string if the initial character is not a forward slash?

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

Requiring Subclasses to Maintain Immutability

I have created a base class that contains various properties: class Component { readonly id: number readonly type: number } Now, I am looking to create some subclasses, such as: class HealthComponent extends Component { max_health: number, ...

Form Validation using AJAX, Jquery, and PHP

Struggling with the jQuery code below. It is intended to validate form input upon submission. The password seems to be set correctly, but on the first submit, err is true. When submitting a second time (with an unchanged password), err is properly set to f ...

Unusual issue: Unhandled error of type TypeError: ajax invocation deemed illegal

UPDATE The issue has been resolved. It appears that I made a mistake by not including .value after referencing the title element. I have been using ajax for several months now, and this error is new to me. I am creating an array of formData and sending ...

organize and identify a JSON data using sorting and matching techniques

Having a JSON structure as shown below: [ { "id": "1", "freq": "1", "value": "Tiruchengode", "label": "Tiruchengode" }, { "id": "2", "freq": "1", "value": "Coimbatore", "label": " ...

The success function in Ajax jQuery isn't being triggered even though the code is running

When using AJAX, I am trying to pass a json variable. Here is the ajax function I have: <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> <script> function emailCheckForFrontend() { var key='7 ...

Retrieve AJAX images but experiencing issues with jQuery click function

In the index HTML file, I have 7 images. I have implemented a jQuery function that gets the ids of the images when they are clicked. The jQuery function looks like this: $(".imgss").click(function() { alert($(this).attr("id")); }); I assign the clas ...

Does the presence of all angular2 bundles in index.html result in imports retrieving data from memory rather than making another request to the server?

Transitioning from angular 1 to angular 2, I initially faced some challenges with performance using a node backend. The page took 40 seconds to fully load in Firefox and made over 500 requests. This made me consider reverting back to angular 1 until the re ...

Get distinct values under the same key from an array of objects

In my Vue application, I received an API response containing information about various products: productData = [{ "id": 4, "name": "product02", "image": "https://example.com/images/example.png", "variations": [{ "id": 1, "pr ...

The incorrect sequence of Angular/Protractor functions is causing issues within the tests

Trying to extract the values from a column in a grid and compare them before and after sorting can be tricky. I have two functions set up for this task - one to retrieve the initial column values, and another to check the order post-sorting. However, there ...

Troubleshooting Issue: Failure of Ajax Script to Display Saved Data in Edit Function

Whenever I clicked on the Edit icon in the action column of my data tables, the saved data did not display as expected. I noticed that this issue was only occurring for file input types, while it worked properly for text input types. In the Blade file Ad ...

I am experiencing an issue with importing my CSS file

Have a somewhat confusing HTML question that is frustrating me. I am struggling to incorporate certain css files into a jsp page. Here is what I have attempted: <link href="<c:url value="/css/bootstrap.css"/>" rel="stylesheet"> And also tri ...

Surfing the web with Internet Explorer means music downloads rather than streaming

My music website functions properly, however I am experiencing issues when accessing it through Internet Explorer. The internet download manager is downloading music from the site without any problems in Chrome and Firefox. Is there a way to prevent or b ...

What is the data type of the $result.rows.item(x) variable in Javascript?

I have a function that reads and displays the contents of a SELECT query from WEBSQL in HTML5. I need to reuse this function, but I am facing an issue because it returns an Array of JSON objects and I want to convert it to rows.item(). Does anyone know how ...

How can you determine the dimensions of an image in HTML using Python?

Is there a way to extract image size information from source code using Python? tree = etree.HTML(source_page_text) image_list = tree.xpath('//img[@src]') The 'img' tags with 'src' attributes can be found using xpath as show ...

Display list items in HTML based on the length of an array

In my backend, I have a user-defined array of cars. If the user selects 3 cars, the array will contain 3 elements. I would like to display specific details of the cars in HTML elements within a list. The array is based on JavaScript. Here is an example of ...

Importing D3 data from CSV files using the "%" symbol

I am trying to import a CSV file with the following data: Month, Ratio January, 0.19% February, 0.19% March, 0.19% April, 0.18% The current code snippet I'm using is as follows: d3.csv("month_ct.csv", function(d) { return { month: d ...

Effective methods for eliminating timezone in JavaScript

I need to display the time and format {{transaction.submitTime | date:'yyyy-MM-dd HH:mm:ss Z'}} Currently, it displays 2015-04-23 02:18:43 +0700 However, I would like to show the time without +0700, where the hour will be incremented by 7. Is ...

Tips for creating <table> HTML code without relying on JSF tag libraries like h:datatable or ui:repeat, while still utilizing JSF for managing page navigation

I am facing a challenge with rendering tables in my application. The tables have 12 columns and up to 1800 rows, taking 8 seconds to display using h:dataTable in JSF. I attempted using ui:repeat with a Java List object in JSF for row data retrieval, which ...

Explore the power of Infinity.js for optimizing the rendering of large HTML tables, complete with a detailed example using prototype

Is there a way to efficiently load/render large html tables without compromising performance, especially in Internet Explorer? I recently came across a plugin in prototype.js (https://github.com/jbrantly/bigtable/, post: , demo:) that addresses this issue ...

Menu/navigation bar designed with floating lines and an array of color options

I'm currently working on implementing this specific menu into my Wordpress site. My main concern is figuring out how to customize the hover effect for each navigation item. Currently, the float line changes to red (background-color:#800; height:2px;) ...