How to Remember which Tab was Open with jQueryUI and Cookies CodeUnit 13 FEB 2013

jqueryui logoIt is often quite useful on page reload or when coming back to a previously visited page, to open up that page with the last tab that was being viewed still activated. In the past jQuery UI tabs made this fairly easy through browser cookies being baked into the tabs initialization call.

Unfortunately, with the release of jQuery UI 1.10, the cookie option for tabs has been deprecated, rendering all your previous statements that looked like this, pretty much useless:

cookie: {
name: 'mytabs',
expires: 1

From the official upgrade path notes:

Support for cookies is not part of the core functionality for tabs and there are many ways to manage state across page loads. Therefore, the cookie option has been deprecated in favor of setting the active option appropriately during initialization.

Luckily for us though, just because native tab support for cookies is gone doesn’t mean that we can’t still remember which tab was open using cookies – we just need to alter our approach first!

The idea is simple enough. As before, use a cookie to store the currently activated tab index. On load, if the cookie exists, activate the associated tab index. If it doesn’t exist, load the first one. Finally, on tab activate, update the cookie with the new index value.

In practice:

var tabCookieName = "mytabs";
active : ($.cookie(tabCookieName) || 0),
activate : function( event, ui ) {
var newIndex = ui.newTab.parent().children().index(ui.newTab);
$.cookie(tabCookieName, newIndex, { expires: 1 });

Simple, but works like a charm!

Related Posts:

About Craig Lotter

South African software architect and developer at Touchwork. Husband to a cupcake baker and father to two little girls. I don't have time for myself any more.

  • dr java


  • please post the complete code, including html.

  • Sergio

    You are right, works like a charm!