Tabs event clicking on active tab

11,369

Solution 1

Try this:

$("#tabs")
    .tabs()
    .on("click", '[role="tab"]', function() {
        $(this).closest("ul") // The current UL
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css">
<div id="tabs" class="ui-layout-center">
  <ul>
    <li>
      <a href="#tab-1">Tab #1</a>
    </li>
    <li>
      <a href="#tab-2">Tab #1</a>
    </li>
  </ul>
  <div id="tab-container">
    <div id="tab-1">
      tab 1!
    </div>
    <div id="tab-2">
      tab 2!
    </div>
  </div>
</div>

Solution 2

Can this be done using the tabs extra events as I am trying not to to add a separate click event outside the ui methods

No, as far as I know. I checked the documentation and there is no event that fires when you click on a already activated tab.

However if you are looking for other solutions then I can suggest you to use below code to apply the click event.

NOTE: make sure to add this line of code before applying the tab plugin. Because we want to run our script first and then the jquery plugin script, else the plugin will make the clicked tab as active and then when our script runs it evaluates to active tab which is wrong.

$('#tabs li a').on('click',function(){
  if($(this).closest('li').hasClass('ui-state-active')){
      alert('this tab is already active');
  }
});

Here is a working DEMO

$('#tabs li a').on('click',function(){
  if($(this).closest('li').hasClass('ui-state-active')){
      alert('this tab is already active');
  }
});

$("#tabs").tabs({
   beforeActivate: function(event, ui) {
      
   }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css">
<div id="tabs" class="ui-layout-center">
  <ul>
    <li>
      <a href="#tab-1">Tab #1</a>
    </li>
    <li>
      <a href="#tab-2">Tab #1</a>
    </li>
  </ul>
  <div id="tab-container">
    <div id="tab-1">
      tab 1!
    </div>
    <div id="tab-2">
      tab 2!
    </div>
  </div>
</div>
Share:
11,369

Related videos on Youtube

Pete
Author by

Pete

Too many downvoters of correct answers ruining SO. Just because it's not how you would do it, doesn't mean it's wrong or not useful.

Updated on June 04, 2022

Comments

  • Pete
    Pete almost 2 years

    I'm using jquery ui tabs and was wondering if there is an event for when you click on an active tab (or any tab).

    I have tried the activate and beforeActive but these only seem to fire when a non active tab is clicked on (see following code)

    $("#tabs").tabs({
      beforeActivate: function(event, ui) {
        console.log('beforeActivate')
      },
      activate: function(event, ui) {
        console.log('activate')
      }
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script type="text/javascript" src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
    <link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css">
    <div id="tabs" class="ui-layout-center">
      <ul>
        <li>
          <a href="#tab-1">Tab #1</a>
        </li>
        <li>
          <a href="#tab-2">Tab #1</a>
        </li>
      </ul>
      <div id="tab-container">
        <div id="tab-1">
          tab 1!
        </div>
        <div id="tab-2">
          tab 2!
        </div>
      </div>
    </div>

    What I'm attempting to do is toggle a class on the ul when the active li is clicked. Can this be done using the tabs extra events as I am trying not to to add a separate click event outside the ui methods

  • Pete
    Pete over 7 years
    sorry, was just editing my question to say I've tried that too - only fires when a non active tab is clicked
  • Pete
    Pete over 7 years
    hmm nice, you might be onto something here. Not sure who downvoted you but +1 from me
  • Pete
    Pete over 7 years
    I tried this earlier but it seems to fire whenever any tab is clicked, whether it has that class or not
  • Rajshekar Reddy
    Rajshekar Reddy over 7 years
    @Pete no it will not.. because I have added this class li.ui-state-active it works only on the active tab. Let me ad a working demo for you
  • Pete
    Pete over 7 years
    here is my demo: jsfiddle.net/MhEEH/399. No matter which tab you click (active or not) it fires the console.log
  • Rajshekar Reddy
    Rajshekar Reddy over 7 years
    @Pete you are right.. I will resolve it.. give me a minute
  • Pete
    Pete over 7 years
    nice, didn't even think about using hasClass as I would have thought it would have had it seeing as though the previous selector seemed to fire all the time
  • Marcos Pérez Gude
    Marcos Pérez Gude over 7 years
    Sorry, was me. Before the edition, I was doing a test with the code and it doesn't works for me... but if OP said that's fine, it's ok :D