Event listeners for jQuery's UI tabs?
Asked Answered
C

5

9

Are there event listeners available for jQuery UI's tabs widget?

I'm wanting to change the background colour on a web page depending on what tab index is currently active. So something like this (pseudo code):

$('.tabs').addEventListener(index, changeBackgroundImage);

function changeBackgroundImage(index) {
    switch (index) {
        case 1:
            $('body').css('background-image', '/images/backgrounds/1.jpg');
        break;
        case 2:
            $('body').css('background-image', '/images/backgrounds/2.jpg');
        break;
        case 3:
            $('body').css('background-image', '/images/backgrounds/3.jpg');
        break;
        default:
            $('body').css('background-image', '/images/backgrounds/default.jpg');
        break;
    }
};
Ceolaceorl answered 5/4, 2011 at 11:40 Comment(0)
M
9

Use tabsshow event, Index will be start from 0.

$('#tabs').bind('tabsshow', function(event, ui) { 
  switch (ui.index){
    case 0: 
        $('body').css('background-image', '/images/backgrounds/1.jpg');
        break;
  }
});
Montfort answered 5/4, 2011 at 11:53 Comment(0)
P
17

it seems the old's version's of jquery ui don't support select event anymore.

This code will work with new versions:

$('.selector').tabs({
                    activate: function(event ,ui){
                        //console.log(event);
                        console.log(ui.newTab.index());
                    }
});
Pallor answered 15/3, 2013 at 14:35 Comment(0)
S
12

Use tabsactivate event

$('#tabs').on('tabsactivate', function(event, ui) {
    var newIndex = ui.newTab.index();
    console.log('Switched to tab '+newIndex);
});
Sable answered 11/3, 2013 at 19:12 Comment(0)
M
9

Use tabsshow event, Index will be start from 0.

$('#tabs').bind('tabsshow', function(event, ui) { 
  switch (ui.index){
    case 0: 
        $('body').css('background-image', '/images/backgrounds/1.jpg');
        break;
  }
});
Montfort answered 5/4, 2011 at 11:53 Comment(0)
I
1

Yep: http://jqueryui.com/demos/tabs/ under "Events"

Working example: http://jsfiddle.net/g7Q2L/ (I used embedded values and not the index to make the markup less coupled with the code)

Check the docs, you can .bind( "tabsselect", function(){}) or when you initiate tabs add a select property to the initiliasing object like in my jsfiddle example.

Insectivorous answered 5/4, 2011 at 12:2 Comment(0)
H
0

the Tabs plugin have a 'show' event which is fired whenever a tab is shown.

check the events in documentation > http://jqueryui.com/demos/tabs/

Hilariahilario answered 5/4, 2011 at 11:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.