Looping through dyamically generated jQuery tabs and adding classes -


ok, have tabbed interface adds tabs dynamically once link clicked , fills newly generated tabs using ajax. portion works fine, need extend functionality allow dynamically-created tabs have class assigned them during same click. current code follows:

$(document).ready(function() {     var tabs = $("#tabs").tabs();     $(".tabbutton").live("click", function(){         var gettopicid = $('.topicid',$(this).parent()).val();         tabs.tabs('add', 'ajax.php?section=summary&id=' + gettopicid, 'summary');         tabs.tabs('add', 'ajax.php?section=read&id=' + gettopicid, 'read topic');     }); }); 

this code works absolutely fine add tabs. however, i'm not sure how go adding class. i've tried placing following directly after second tabs.tabs line, no avail:

$(this).addclass('testclass'); 

the main problem generated tabs given id of #ui-tabs-* * automatically incremented value in form of number. should point out reason assigning class can destroyed @ later date. did consider using each function, wasn't sure if best method possible?

apologies if seems bit of daft question, jquery isn't strong point.

edit

ok, following on bumble's suggestion below, i've added little more functionality, namely removal of created tabs same onclick event:

$(document).ready(function() {     var tabs = $("#tabs").tabs();     $(".tabbutton").live("click", function(){         $(\'div[id*="ui-tabs-"]\').each(function(index) {             if($(this).hasclass(\'testclass\'))             $("#tabs").tabs("remove", \'.testclass\');         });         var gettopicid = $(\'.topicid\',$(this).parent()).val();         tabs.tabs(\'add\', \'ajax.php?section=summary&id=\' + gettopicid, \'summary\');         tabs.tabs(\'add\', \'ajax.php?section=read&id=\' + gettopicid, \'read topic\');         $(\'div[id*="ui-tabs-"]\').each(function(index) {             if(!$(this).hasclass(\'testclass\'))             $(this).addclass(\'testclass\');         });     }); }); 

now code works, degree. ui has 1 tab persistent (the main tab containing onclick link), 2 tabs generated said link. however, code above removes persistent tab , leaves 2 generated ones intact! ideas?

further edit

ok, after looking @ documentation, have provide remove function tab index remove, can't remove tabs based on class name. somehow remove function has first grab index of tab (if has testclass class), remove it. reading right?

you can use wildcard select generated tabs , loop through, checking if have class or not:

$(document).ready(function() {     var tabs = $("#tabs").tabs();     $(".tabbutton").live("click", function(){         var gettopicid = $('.topicid',$(this).parent()).val();         tabs.tabs('add', 'ajax.php?section=summary&id=' + gettopicid, 'summary');         tabs.tabs('add', 'ajax.php?section=read&id=' + gettopicid, 'read topic');         $('div[id*="ui-tabs-"]').each(function(index) {             if($(this).hasclass('testclass'))                 $(this).remove();             else                 $(this).addclass('testclass');         });     }); }); 

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -