Since the jquery-ui tab works with ids instead of class tags, I created a function to generate ids for all my classes. Now I want to add new content and apply the same to that new content, but after adding new content all other tabs stop working!
This is the html content:
<div class="accordion">
<h3>Product x</h3>
<div class="tabs">
<ul>
<li><a href="" class="tabpanellink">General product info</a></li>
<li><a href="" class="tabpanellink">End user info</a></li>
</ul>
<div class="tabpanel">
general info for product x.
</div>
<div class="tabpanel">
end user info product x.
</div>
</div>
</div>
<div class="accordion">
<h3>Product y</h3>
<div class="tabs">
<ul>
<li><a href="" class="tabpanellink">General info</a></li>
<li><a href="" class="tabpanellink">End user info</a></li>
</ul>
<div class="tabpanel">
general info for product y.
</div>
<div class="tabpanel">
end user info product y.
</div>
</div>
</div>
And this is what I'm trying to do with it:
function fixtabs(){
$(".tabpanellink").each(
function(uniqueindex){
$(this).attr('href', '#tab-' + uniqueindex);
}
);
$(".tabpanel").each(
function(uniqueindex){
$(this).attr('id', 'tab-' + uniqueindex);
}
);
$(".accordion").accordion({
collapsible: true,
active: false
});
}
function updateDocument(){
fixtabs();
$(".tabs").tabs();
}
function addProduct(){
//Add another product(this is a server response in a real case scenario):
var newProduct = '';
newProduct += '<div class="accordion">';
newProduct += '<h3>Product z</h3>';
newProduct += '<div class="tabs">';
newProduct += '<ul>';
newProduct += '<li><a href="" class="tabpanellink">General product info</a></li>';
newProduct += '<li><a href="" class="tabpanellink">End user info</a></li>';
newProduct += '</ul>';
newProduct += '<div class="tabpanel">';
newProduct += 'general info for new product.';
newProduct += '</div>';
newProduct += '<div class="tabpanel">';
newProduct += 'end user info product x.';
newProduct += '</div>';
newProduct += '</div>';
newProduct += '</div>';
$(".accordion").first().before(newProduct);
updateDocument();
}
updateDocument();
addProduct();
Here is the jsfiddle.
How to apply the tab effect on the new content without corrupting the existing tabs?
Update in response to answers: I can't remove updateProduct();
! I have to call updateDocument first so everything is initialized! addProduct is only called afterwards (i.e. when user clicks add product!)