jQuery UI: Remember Your Open Tab CodeUnit 25 NOV 2010

The jQuery UI library is a brilliant collection of official jQuery UI widgets and effects, one of them being the extremely useful Tabs widget.

Tabs allows you to shorten a page by placing bits of a page’s content into their own little “tabs”, which can be access by clicking on the tab header. A brilliant space saving mechanism and one that has become quite commonplace in applications these days.

Sometimes it is nice to remember what tab you currently have open so that when you travel to some other part of a site and then return to your original page, you want it to remember which tab you had currently selected.

Thankfully achieving this with jQuery UI’s tabs implementation is particularly easy.

Say hello to good old browser cookies.

To implement this built in functionality, you will need to include over and above the required jquery-ui.js and jquery.js libraries, the jquery.cookie.js plugin at the head of your page. This simplifies interaction with browser cookies and also provides a path for the tabs widget to use in order to save its state.

With this down, enabling tab state caching is a simple as declaring the tabs with a valid “cookie” option declaration:

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

And this it. Simple as pie really. Note the ‘name’ option passed to the cookie object. This is important as it allows you to have different cookies for your different tabs instantiations on the site.

Useful, eh? ;)

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.

  • Anshul Kumar Chawla

    I tried but the same thing is not working. in my case.