Mark Needham

Thoughts on Software Development

jQuery UI Tabs: Changing selected tab

with 13 comments

We’re using the tabs part of the jQuery UI library on the project I’m currently working on and one thing we wanted to do was change the default tab that was being selected.

The documentation suggested that one way to do this was to give the index of the tab we wanted selected when calling the tabs function:

$( ".selector" ).tabs({ selected: 3 });

Since we wanted to select the tab by name based on a value from the query string we thought it would probably be simpler if we could just set the selected tab using a css class.

Our initial thought was that we could put the ‘ui-tabs-hide’ class on the divs that we wanted to hide and then not put that class on the one that we wanted to show.

Unfortunately that didn’t work and the first tab was still being selected…

We downloaded version 1.8.2 of the library via Google’s CDN (which seems really cool!) and were able to see that our class was actually intentionally being removed!

if (o.selected >= 0 && this.anchors.length) { // check for length avoids error when initializing empty list

Luckily a little further down the file there is a comment which explains some other ways to manipulate the selected tab:

			// Selected tab
			// use "selected" option or try to retrieve:
			// 1. from fragment identifier in url
			// 2. from cookie
			// 3. from selected class attribute on <li>

We need to put the class ‘ui-tabs-selected’ on the appropriate <li> and then that will be the one that gets selected.

Be Sociable, Share!

Written by Mark Needham

September 8th, 2010 at 6:32 pm

Posted in jQuery

Tagged with

  • Franky

    Why no just use the name then?

    since you can use the key…

  • Oh neat, didn’t know you could do it using the key. Certainly a good alternative!

  • This works as well
    $j(“#selector”).tabs(“option”, “selected”, 5);

  • Glen

    Thanks, just what I needed.

  • Ciara

    Mark – You just made my day!

  • Pingback: JQueryUI Tab – The Uncola « TechnoBuzz()

  • Siro

    thank you for this

  • Glen Ainscow

    The class is now named ui-tabs-active.

  • Ruchi

     doesn’t work here ?

  • chithu

    Thanks it worked

  • Daniel

    $( “.selector” ).tabs({ active: 3 });

  • eng

    doesn’t work 🙁

  • Stunning Vasu

    dfsgftbazhrruhygghjklsdkksajhfhjewuifjskdmSJHGAyeuiowqorlkmwqnb dJGYUSHJIKODLMZXNBGAVBsvxhggudyaUHEWJNBBV HidjISAJIjnmnnmjhoi;iokljndbs MA XpWx