views:

125

answers:

2

I'm using the JQuery Tabs.

I want to fire a function of mine when the user clicks on a particular tab.

How do I bind an event to each individual JQuery Tab when clicked?

+3  A: 

Select event: This event is triggered when clicking a tab.

If you want to add a different function to each tab, you can just give each anchor an onclick event.

<ul>
    <li><a href="#tabs-1" onclick="function1();">Nunc tincidunt</a></li>
    <li><a href="#tabs-2" onclick="function2();">Proin dolor</a></li>
    <li><a href="#tabs-3" onclick="function3();">Aenean lacinia</a></li>
</ul>
....

<script type="text/javascript">

    $(document).ready(function(){
        $( ".selector" ).tabs();
    });

    function function1(){
        // your code here
    }

    function function2(){
        // your code here
    }

    function function3(){
        // your code here
    }
</script
R0MANARMY
I don't think I explained well enough. Let's assume I have 4 tabs. I want to fire a DIFFERENT function per each of the 4 tabs. The Events function seems to only be able to fire the SAME code for all tabs selected ... not a particular tab individually
Nicki
A: 

How about something like this:

$('#example').tabs({
    select: function(event, ui) {
        var $tabs = $('#example').tabs();

        // Gets the currently selected tab.
        var selected = $tabs.tabs('option', 'selected');

        // Run a switch on that selected tab and do what you need depending
        // on which tab was selected.
        switch(selected){
            case 0:
              break;
            case 1:
              break;
            case 2:
              break;
            case 3:
              break;
        }   
    }
});

There might be a prettier solution but this should technically do what you need.

EDIT: This works perfectly fine if you are using static tabs. If you have the possiblity of dynamic tabs then use something like this:

$("#tabID").click(function(){
    do something here...;
});
FallenRayne
That's all kinds of evil. You're better off just putting the function name as an attribute onto your anchors, retrieving it using var fname = $(ui.tab).attr("[whatever you called the attribute]"), finding it in the list of functions and executing it window[fname]()
R0MANARMY
Not sure why this would be evil. It is a dynamic way of listening for one event firing and determining which tab was selected on the fly, then calling the proper function or block of code. This is the proper way to react to an event firing. By using an inline "onclick" you are doing a similar thing, but it is harder to maintain and more prone to issues. The JavaScript community as a whole has been moving away from inline JavaScript for a while now.
FallenRayne
It's evil because you're linking behavior to an arbitrary index rather than to elements that they are related to. It would also break spectacularly if you later add ability to rearrange tabs or have conditional tabs.
R0MANARMY
Ok, if there is a chance of dynamic tabs then you are correct. If the tabs are static then what I posted is perfectly fine. Either way, I edited my answer so it has an answer for going off of the id.
FallenRayne