dynamically remove tab

dynamically remove tab

Hello,

I dynamically added 2 tabs by calling the  addTab function:
  1. $(function() {
  2.     var tabTitle = $( "#tab_title" );
  3.     var tabContent = $( "#tab_content" );
  4.     var tabTemplate = "<li><a href='#{href}'>#{label}</a> <span class='ui-icon ui-icon-close' role='presentation'>Remove Tab</span></li>";
  5.     var tabCounter = 2; 
  6.     var tabs = $( "#tabs" ).tabs();
  7.    
  8. function addTab(tabTitle, tabContent) {
  9.       var label = tabTitle || "Tab " + tabCounter;
  10.       var id = "tabs-" + tabCounter;
  11.       var li = $( tabTemplate.replace( /#\{href\}/g, "#" + id ).replace( /#\{label\}/g, label ) );
  12.       var tabContentHtml = tabContent || "Tab " + tabCounter + " content.";

  13.       tabs.find( ".ui-tabs-nav" ).append( li );
  14.       tabs.append( "<div id='" + id + "'><p>" + tabContentHtml + "</p></div>" );
  15.       tabs.tabs( "refresh" );
  16.       tabCounter++;
  17.     }

How can i remove/close the dynamically added tabs? i tried the following approach, but it does not remove the tabs...
  1. // Remove the panel
  2. $( "#" + "tabs-2").remove();
  3. // Refresh the tabs widget
  4. tabs.tabs( "refresh" );

  5. // Remove the panel
  6. $( "#" + "tabs-3").remove();
  7. // Refresh the tabs widget
  8. tabs.tabs( "refresh" );