Matt
Matt

Reputation: 1574

jQuery UI Tabs - Event handler not added if tab left and switched back to

I have a jQuery UI tab control where the first time a tab is opened I can add an event handler (in $().ready()) and it works fine.

If I switch to a different tab and then switch back again then i'm sturggling to maintain the event handler. I can either choose between adding it each time and it fires once for every time it's added, or adding each time but it only fires the first time the tab is opened.

Option 1 - Two event handlers

$(document).on("change", "#chkIsBreak", function () { alert("hi");});

Will cause the event to fire once the first time the tab is loaded, but every time I switch away and back to the tab it adds the same event handler, so you end up with an extra alert each time.

If i look at the events with jQuery._data($(document).get(0), "events"); I can see the event has been added twice.

Option 2 - One event handler, works only first time it's added.

$("#chkIsBreak").on("change", function () { alert("hi");});

Works fine the first time the tab is opened, but when switching tabs and then back again the event won't fire at all.

If i look at the event with jQuery._data($("#chkIsBreak").get(0), "events"); I can see the event has been added, but it won't fire unless it's the first time the tab has been opened.

Any suggestions? I could use option 1 and check the jQuery._data(), but this is supposed to be internal jQuery data. What's the right thing to do here?

Upvotes: 1

Views: 108

Answers (1)

emmanuel
emmanuel

Reputation: 9615

There is a beforeActivate and beforeLoad event that you could use.

$(function() {
  $("#tabs").tabs({
    beforeActivate: function(event, ui) {
      alert("hi");
    }
  });
});
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<div id="tabs">
  <ul>
    <li><a href="#tabs-1">Nunc tincidunt</a>
    </li>
    <li><a href="#tabs-2">Proin dolor</a>
    </li>
    <li><a href="#tabs-3">Aenean lacinia</a>
    </li>
  </ul>
  <div id="tabs-1">
    <p>Proin elit arcu, rutrum commodo, vehicula tempus, commodo a, risus. Curabitur nec arcu. Donec sollicitudin mi sit amet mauris. Nam elementum quam ullamcorper ante. Etiam aliquet massa et lorem. Mauris dapibus lacus auctor risus. Aenean tempor ullamcorper
      leo. Vivamus sed magna quis ligula eleifend adipiscing. Duis orci. Aliquam sodales tortor vitae ipsum. Aliquam nulla. Duis aliquam molestie erat. Ut et mauris vel pede varius sollicitudin. Sed ut dolor nec orci tincidunt interdum. Phasellus ipsum.
      Nunc tristique tempus lectus.</p>
  </div>
  <div id="tabs-2">
    <p>Morbi tincidunt, dui sit amet facilisis feugiat, odio metus gravida ante, ut pharetra massa metus id nunc. Duis scelerisque molestie turpis. Sed fringilla, massa eget luctus malesuada, metus eros molestie lectus, ut tempus eros massa ut dolor. Aenean
      aliquet fringilla sem. Suspendisse sed ligula in ligula suscipit aliquam. Praesent in eros vestibulum mi adipiscing adipiscing. Morbi facilisis. Curabitur ornare consequat nunc. Aenean vel metus. Ut posuere viverra nulla. Aliquam erat volutpat.
      Pellentesque convallis. Maecenas feugiat, tellus pellentesque pretium posuere, felis lorem euismod felis, eu ornare leo nisi vel felis. Mauris consectetur tortor et purus.</p>
  </div>
  <div id="tabs-3">
    <p>Mauris eleifend est et turpis. Duis id erat. Suspendisse potenti. Aliquam vulputate, pede vel vehicula accumsan, mi neque rutrum erat, eu congue orci lorem eget lorem. Vestibulum non ante. Class aptent taciti sociosqu ad litora torquent per conubia
      nostra, per inceptos himenaeos. Fusce sodales. Quisque eu urna vel enim commodo pellentesque. Praesent eu risus hendrerit ligula tempus pretium. Curabitur lorem enim, pretium nec, feugiat nec, luctus a, lacus.</p>
    <p>Duis cursus. Maecenas ligula eros, blandit nec, pharetra at, semper at, magna. Nullam ac lacus. Nulla facilisi. Praesent viverra justo vitae neque. Praesent blandit adipiscing velit. Suspendisse potenti. Donec mattis, pede vel pharetra blandit, magna
      ligula faucibus eros, id euismod lacus dolor eget odio. Nam scelerisque. Donec non libero sed nulla mattis commodo. Ut sagittis. Donec nisi lectus, feugiat porttitor, tempor ac, tempor vitae, pede. Aenean vehicula velit eu tellus interdum rutrum.
      Maecenas commodo. Pellentesque nec elit. Fusce in lacus. Vivamus a libero vitae lectus hendrerit hendrerit.</p>
  </div>
</div>

Upvotes: 0

Related Questions