I am using jquery to create tabs on my GUI. My question is, I want to execute a function upon clicking on one of the tabs. I found the following info from the jquery site. I am new to jquery, however, and I can’t understand what “.selector” means. How can I identify a specific tab’s name? I only have an id for the whole tab set. Also, there are two segments of code from the jquery website. Can I choose to use either one? Thanks!
Directly from the jquery UI website:
This event is triggered when clicking a tab.
Code examples
Supply a callback function to handle the select event as an init option.
$( ".selector" ).tabs({
select: function(event, ui) { ... }
});
Bind to the select event by type: tabsselect.
$( ".selector" ).bind( "tabsselect", function(event, ui) {
...
});
First, a selector is basically some way to select a DOM element. The basic selectors follow CSS and regular JavaScript Format. # for id and . for class
So in jQuery, to select a div with the id of “my-div” you would do this:
This grabs the jQuery node, with which, you can modify the node pretty much however you desire. Read about selectors more in detail in the jQuery documentation.
Now, for your tab question…you probably want the “show” event, not “select” so that you can catch the tab you are entering. The following example will set up a couple tabs, then alert the index of which tab you just clicked on.
HTML
JavaScript
To explain the demo…You select the DOM element with id “tabs” then apply the jQuery UI plugin “tabs” to it. This modifies the HTML markup to be aesthetically pleasing, as well as provides you with an event system to give you flexibility and control over your UI. The “show” option is a callback function that should be triggered whenever a new tab is shown. Inside the callback, we use the jQuery UI API to select the tabs again (through this though) and then grab the index of the selected tab. Then of course, we alert that.
Hope this helps you out, and good luck.