Executing JavaScript when a Bootstrap tab is opened

Struggling to implement a JavaScript action when a Bootstrap tab is opened. Unfortunately, the following scripts have not been successful:

$('.nav-tabs a').on('shown.bs.tab', function(){
    vchart.redraw();
});

Also attempted:

$(document).ready(function(){
    $(".nav-tabs a").click(function(){
        $(this).tab('show');
    });
    $('.nav-tabs a').on('shown.bs.tab', function(){
        vchart.redraw();
    });
});

The scripts are within the tabs themselves. Tried:

$('ul.nav a').on('shown.bs.tab', function (e) {

instead of

$('.nav-tabs a').on('shown.bs.tab', function(){

Contains some HTML:

<div class="col-md-9">
  <ul class="nav nav-tabs">
    <li class="active">
      <a data-toggle="tab" href="#1">Tab 1</a>
    </li>
    <li>
      <a data-toggle="tab" href="#2">Tab 2</a>
    </li>
  </ul>
</div>

<div id="1" class="tab-pane fade in active">
  Tab 1 content...
</div>
<div id="2" class="tab-pane">
  Tab 2 content...
</div>

Pondering what could be causing the issue?

Answer №1

To properly handle the event, you need to bind it using addEventListener in vanilla JavaScript.

$('ul.nav a').bind('click', function() { //use bind for any event you want to handle 
  alert('User clicked on "foo."');
  //vchart.redraw();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="col-md-9">
  <ul class="nav nav-tabs">
    <li class="active">
      <a data-toggle="tab" href="#1">Tab 1</a>
    </li>
    <li>
      <a data-toggle="tab" href="#2">Tab 2</a>
    </li>
  </ul>
</div>

<div id="1" class="tab-pane fade in active">
  Tab 1 content...
</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

"Is there a way to extract a value from a JSON object using

I have an object with the following key-value pairs: { 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier': '918312asdasc812', 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name': 'Login1&a ...

bespoke slickgrid dropdown editor

I am currently working on implementing a slickgrid feature where a cell needs to display a dropdown (selectlist) with values fetched from a restful service. These values will vary for each row. The requirement is that the user should be able to select one ...

The curious case of Jade view caching in Express

Recently, I updated my Node.js and Express to versions 0.10.21 and 3.4.4, respectively, and now I'm encountering some strange view caching issues during development (as well as production). It appears that the HTML generated from views included withi ...

Send information using AJAX within a .NET integrated browser

In our current setup, we utilize a .NET embedded browser to showcase an HTML page that is generated by applying an XSLT file to an XML file using .NET. This process results in HTML content displayed within the embedded browser through the DocumentText prop ...

What is the best way to incorporate a scroll bar into a table that updates dynamically?

If I were to create an empty table in my index.html: <body> <table width="800" border="0" class="my-table"> <tr> </tr> </table> </body> Afterward, I would dynamically add rows and columns to my-table using ...

When a user clicks a button, modify a section of the URL and navigate to

I've been searching everywhere for a solution to this issue, but I just can't seem to find it. I'm hoping someone here can help me out. In a bilingual store, I need a way to redirect users to the same product on a different domain when they ...

ReactJS: Error message indicating that the update depth limit has been exceeded

I'm facing an issue with toggling the state of a component in ReactJS. The error message I am receiving says: Maximum update depth exceeded. This can occur when a component repeatedly calls setState within componentWillUpdate or componentDidUpdate. ...

Customizing React components based on API data

const LinkList = () => { const [links, setLinks] = useState([]); const url = 'http://localhost:5000/xyz'; const hook = () => { console.log('effect'); axios .get(url) .then(respo ...

Exploring the diversity of perspectives through Angular

Currently, I am in the process of integrating multiple views in Angular to address the footer issue on a website that I am constructing. I want to ensure that the information I have been studying and attempting to replicate is accurate. Here is what I have ...

Ways to detect the use of vue.js on a webpage without relying on vue-devtools

One way to determine if the page is utilizing Angular or AngularJS is by inspecting window.ng and window.angular. Is there a similar method to identify whether Vue is being used on the page directly from the console, without relying on vue-devtools? ...

What is the method for comparing a value in TypeScript that does not match a given value?

I am new to scripting languages and encountered an issue while using enums with if-else statements in TypeScript. To work around this problem, I have decided to use switch-case instead of if-else conditions. Despite trying !== and !===, they do not seem t ...

Tips for importing modules without encountering errors

Currently in the process of constructing a compact website with socket.io and express. Opting for Typescript to ensure accurate type errors, then transpiling the frontend code to Javascript for seamless browser execution. Frontend code: import { io, Socke ...

Why does it fire off numerous requests even though I only called it once?

Everything seemed to be working fine with my project. However, I noticed in the console network that one of my GET requests is being sent twice even though I only triggered it once. View network console here If I comment out the entire created function co ...

When using CSS float:left and overflow:visible, the text may get cropped-off at

I'm currently experimenting with creating a color gradient in javascript using numerical values within some of the divs to indicate scale. However, I've run into an issue where as the values get larger, they are cut off due to the float:left prop ...

I keep running into an issue whenever I attempt to import material ui icons and core - a frustrating error message pops up stating that the module cannot be found

[I keep encountering this error message when attempting to utilize @material-ui/core and icons] `import React from "react"; import "./Sidebar.CSS"; import SearchIcon from "@material-ui/icons/Search"; const Sidebar = () => { return ( <> ...

Update an array by breaking it into smaller chunks, while maintaining the original index positions

I am faced with a situation where I must update an existing array by utilizing its chunks to perform a specific operation. While I have a functional solution in place, I am concerned about the performance implications and I am eager to determine if my curr ...

three.js Orthographic View: Zoom Whole Scene

I am brand new to using three.js, and I'm struggling to figure out how to create a "zoom all" feature with an OrthographicCamera. Despite searching through the documentation and online resources, all I could find were solutions for PerspectiveCamera t ...

unable to utilize the library three.js

I stumbled upon an interesting animation on Codepen that uses blocks to reconstruct a map image. The necessary JavaScript files are three.min.js and TweenMax.min.js. I copied the links from Codepen and pasted them into my HTML using <script src="">&l ...

Prevent selection of specific weekdays in Javascript

Looking to enhance my calendar functionality in JavaScript by disabling multiple week days. I have successfully disabled Sunday using the code snippet below, which utilizes the getDay() method. Any guidance on how to disable more than one week day with Jav ...

Modifying the cursor appearance during object dragging is a simple task that can enhance

html .container, .container * { cursor: url('img/arrowPointer.png'), auto; } javascript $('html').on('dragstart', function () { $('html').addClass('container'); }); $('html').on('d ...