views:

149

answers:

2

Hi,

I am using jQuery UI Tabs.

<div id="tabs">
<ul id="tablist">
    <li><a href="#fragment-1"><span>One</span></a></li>
</ul>
</div>

I have a button that adds new tabs. I use the following code:

var newTabId = $('#tabs').tabs('option', 'selected') + 1;    
$('#tabs').tabs("add",'someUrl.htm','New Tab',newTabId);

(Tab will be added next to the currently selected tab)

Now none of the newly added tabs fire any events such as a click or hover

$('#tablist li').click(function(){
    alert('test message');
});

But events fire properly for the tabs that were there in the initial source code.

How to resolve?

A: 

You probably need to bind the element to an event when it's created, if it's of any interest .clone(true) will clone an item and maintain it's bind with an event.

ILMV
Clone did not work as expected.. it creates exact replica of the existing tab including the styles.
Amit
Ok. was worth a shot :)
ILMV
+5  A: 

Use live instead.

$('#tablist li').live('click', function(){
    alert('test message');
});

Or better yet, delegate the event

$('#tablist').delegate('li', 'click', function(){
    alert('test message');
});
Matt
+1 for delegate
Giorgi
@Matt, I could not get delegate to work for me in this situation. It did not work for the li on tablist. but if i create a delegate for an element that is outside tablist, it works fine...! Any idea??
Amit
@Amit: You should delegate the event to an element that is registered in the DOM at the time your `delegate` method is executed. Does it work on #tabs?
Matt
@Matt, No.. :( actually both #tabs and #tablist exist in DOM.
Amit
@Amit: My only explanation is that the UI Tabs plugin is preventing the event bubbling, which sucks hard. Have you tried registering the event as a `select` event instead (which is part of the tabs plugin); http://docs.jquery.com/UI/Tabs#event-select?
Matt