0

I am trying to call my function resultin a other function. Is there any way to do that? I attached an example.

Here is my function which I want to call it again, or reach the varaibale:

$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
    var  tab= $(e.target).attr("href");
})

In this function I want to reach the tab variable:

$( "#form" ).submit(function( event ) {
    // I want to call the tab variable here
    });
2
  • Move the tab variable outside and submit function in the same scope. Commented Feb 25, 2018 at 12:48
  • Have you tried just reading the active tab when you need it? $('.nav .active [a[data-toggle="tab"]') Commented Feb 25, 2018 at 13:02

2 Answers 2

1

Store the current tab into a data-attribute of that form #form

$('a[data-toggle="tab"]').on('shown.bs.tab', function(e) {
  var tab = $(e.target).attr("href");
  $("#form").data('current-tab', tab);
});

$("#form").submit(function(event) {
  var currentTab  = this.data('current-tab');
});
Sign up to request clarification or add additional context in comments.

Comments

0

Probably best solution is to remember the last tab that was triggered:

var lastTab = null;
$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
    lastTab = $(e.target).attr("href");
});

$( "#form" ).submit(function( event ) {
    if(lastTab == null) {
        throw new Error("Last tab not set, how to handle this?");
    }
    else {
        // do something with the tab url in `lastTab`
    }
}); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.