I am using Twitter Bootstrap v3 to create “nav-pills”. Each of my tabs has a Bootstrap table that I update the values every 5 seconds. My Java script code for one table update is like below:
function LoadpData()
{
$(function() {
$.getJSON(..........);
return false;
});
};
setInterval( LoadpData, 5000);
Sub section of my HTML file:
<ul class="nav nav-pills" id="Tabs">
<li class="active"><a href="#Press" data-toggle="tab">Pr</a></li>
<li><a href="#Fl" data-toggle="tab">Fl</a></li>
<li><a href="#Te" data-toggle="tab">Te</a></li>
<li><a href="#Pu" data-toggle="tab">Pu</a></li>
<li><a href="#Va" data-toggle="tab">Co</a></li>
</ul>
What I would like to do is to find which tab is active and then update the data only for the table on that tab. So I was thinking of adding an if statement to the top of each LoadData() JS function to check and see if that tab is active or not, if yes then send the .getJSON request to the server to get the values, otherwise do nothing. I searched and found that I should be able to find the active tab by using this selector $(‘#Tabs li.active’). Also each of my tabs has a unique href attribute so using this selector $(‘#Tabs a[href=”#Fl”]’), I should be able to get the jquery object and see if it is the active one or not. I am new to jquery, I appreciate if you could help me to find the solution.
function LoadpData()
{
// if ($('#Tabs li.active') = $('#Tabs a[href="#Fl"]')){
// $(function() {
// $.getJSON(..........);
// return false;
// });
// }
2
Answers
You can use a script like :
You can set up a jquery listener on the group of html values on click.
After that you need to call the
LoadpData()
. Per convention, it is recommended you call theLoadpData
in adocument.ready(function(){})
blockHere’s a jsFiddle that might help: https://jsfiddle.net/superjisan/hnxLLknv/