Dynamic jQuery Tabs - Add, Update, Delete And Sorting

Introduction

 
Sometimes, we may need to create/develop some logic in jQuery to add/delete tabs. We will see how easy we can do that using jQuery Tabs and Bootstrap Tabs both. It has always been fun to play with jQuery, which is very user friendly. So let's get started.
 

Ingredients

  • jQuery (min version of jQuery and UI jQuery JS) 
  • jQuery UI (minified version of jQuery UI JS) 
  • jQuery UI CSS
  1. <script src="https://code.jquery.com/jquery-3.5.1.min.js"  
  2.         integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0="  
  3.         crossorigin="anonymous"></script>  
  4.   
  5. <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"  
  6.         integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU="  
  7.         crossorigin="anonymous"></script>  
  8.   
  9. <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">  
What Will We Do?
 
We will perform some basic CRUD operations on a jQuery Tab along with the sorting tab order :
  • Add a Dynamic Tab
  • Delete the Tab
  • Sort Tabs with the Sort Order
HTML
  1. <div class="mainbox">    
  2.     <h1>    
  3.         <b style="color:#9ee0ff;">jQuery Tab</b> Add, Delete and Sorting Example    
  4.     </h1>    
  5.     <div id="tabs">    
  6.         <ul>    
  7.             <li id="1"><a href="#tab-1">Default</a></li>    
  8.         </ul>    
  9.         <div id="tab-1">    
  10.             <p>    
  11.                 Default Tab Content    
  12.             </p>    
  13.         </div>    
  14.     </div>    
  15.     <p>    
  16.         <button id="showDialog" class="ui-button ui-widget ui-corner-all">+ Add Tab</button>    
  17.         <button id="removeTabs" title="Click to Remove Active Tab" class="ui-button ui-widget ui-corner-all">x Remove Tab</button>    
  18.         <button id="showSortable" class="ui-button ui-widget ui-corner-all">Get Sort Order</button>    
  19.     </p>    
  20.     <p style="margin-top:50px;">    
  21.         By Darshan Shah <a href="https://www.c-sharpcorner.com/members/darshan-shah" target="_blank">https://www.c-sharpcorner.com/members/darshan-shah</a>    
  22.     </p>    
  23. </div>    
  24.     
  25. <div id="divDialog" style="display:none;padding:10px;">    
  26.     <div>    
  27.         <input type="text" required class="txt" placeholder="Enter new tab name" />    
  28.     </div>    
  29.     
  30.     <div>    
  31.         <button type="button" id="addTabs" class="ui-button ui-widget ui-corner-all">Submit</button>    
  32.     </div>    
  33. </div>     
User Defined CSS
  1. <style>  
  2.     .mainbox {  
  3.         width70%;  
  4.         margin0 auto;  
  5.         margin-top30px;  
  6.         font-familyArial;  
  7.     }  
  8.   
  9.         .mainbox h1 {  
  10.             background-color#808080;  
  11.             colorwhite;  
  12.             font-size14px;  
  13.             padding10px;  
  14.             text-aligncenter;  
  15.             border-radius: 5px;  
  16.         }  
  17.   
  18.         .mainbox p {  
  19.             text-aligncenter;  
  20.         }  
  21.   
  22.     .ui-widget-overlay.custom-overlay {  
  23.         background-colorblack;  
  24.         background-imagenone;  
  25.         opacity: 0.7;  
  26.         z-index1040;  
  27.     }  
  28.   
  29.     .txt{  
  30.         border:solid 1px #eee;  
  31.         padding:5px;  
  32.         outline:none;  
  33.         margin-bottom:10px;  
  34.     }  
  35.   
  36.     .txt:focus {  
  37.         border:solid 1px #0094ff;  
  38.          
  39.     }  
  40. </style>  
Scripts
  1. <script type="text/javascript">  
  2.   
  3.     // Make tab UI activated using below script  
  4.     var $tabs = $('#tabs').tabs();  
  5.   
  6.     // Open jQuery Dialog to open modal popup - here we ask for tab name from user  
  7.     $("#showDialog").click(function () {  
  8.         $("#divDialog input").val("").focus();  
  9.         $("#divDialog").dialog({  
  10.             title: 'New Tab', modal: true, open: function () {  
  11.                 $('.ui-widget-overlay').addClass('custom-overlay');  
  12.             }  
  13.         });  
  14.     });  
  15.   
  16.     // Adding new Tab on button click  
  17.     $("#addTabs").click(function () {  
  18.   
  19.         // Checking textbox is empty or not  
  20.         if ($.trim($("#divDialog input").val()) == "") {  
  21.             $("#divDialog input").val("").focus();  
  22.         }  
  23.         else {  
  24.   
  25.             // Checking tab name already exist or not  
  26.             var tabNameExists = false;  
  27.             $('#tabs ul li a').each(function (i) {  
  28.                 if ($.trim(this.text.toLowerCase()) == $.trim($("#divDialog input").val().toLowerCase())) {  
  29.                     tabNameExists = true;  
  30.                 }  
  31.             });  
  32.   
  33.             //code to insert new tab here if tab name does not exist  
  34.             if (!tabNameExists) {  
  35.   
  36.                 // Here we are getting max id so that we can assing new id to new tab  
  37.                 var maxid = 0;  
  38.                 $('#tabs ul li').each(function () {  
  39.                     var value = parseInt($(this).attr('id'));  
  40.                     maxid = (value > maxid) ? value : maxid;  
  41.                 });  
  42.   
  43.                 var newid = maxid + 1;  
  44.   
  45.                 // Adding new "<li>" with anchor tag  
  46.                 $("#tabs ul").append(  
  47.                     "<li id='" + newid + "'><a href='#tab-" + newid + "'>" + $("#divDialog input").val() + "</a></li>"  
  48.                 );  
  49.   
  50.                 // Adding Div for content for the above "li" tag  
  51.                 $("#tabs").append(  
  52.                     "<div style='display:none;' id='tab-" + newid + "'><p>Content for Tab: " + $("#divDialog input").val() + "</p></div>"  
  53.                 );  
  54.   
  55.                 // Refreshing the tab as we have just added new tab  
  56.                 $("#tabs").tabs("refresh");  
  57.   
  58.                 // Make added tab active  
  59.                 $("#tabs").find('li a[href="#tab-' + newid + '"]').trigger("click");  
  60.   
  61.                 $("#divDialog").dialog("close");  
  62.             }  
  63.             else {  
  64.                 // Showing message if tab name already exist  
  65.                 alert("Sorry! Tab name already exist");  
  66.                 $("#divDialog input").focus();  
  67.             }  
  68.         }  
  69.     });  
  70.   
  71.   
  72.     // Remove Active Tab on button click  
  73.     $("#removeTabs").click(function () {  
  74.   
  75.         // Confirm from user that he/she sure wants to delete this active tab  
  76.         if (window.confirm("Are you sure want to delete this active Tab?")) {  
  77.             // Find name of Tab by attribute id  
  78.             var tabIndex = $("#tabs .ui-tabs-panel:visible").attr("id");  
  79.   
  80.             // Removing Li and as well as content Div for the specific Tab  
  81.             $("#tabs").find(".ui-tabs-nav li a[href='#" + tabIndex + "']").parent().remove();  
  82.             $("#tabs").find("div[id=" + tabIndex + "]").remove();  
  83.   
  84.             // One removing process done we refresh the tab again  
  85.             $("#tabs").tabs("refresh");  
  86.         }  
  87.     });  
  88.   
  89.     // Here we are making jQuery Tabs (li tag) Sortable  
  90.     $(function () {  
  91.         $("#tabs ul").sortable({ containment: "#tabs ul" });  
  92.         $("#tabs ul").disableSelection();  
  93.     });  
  94.   
  95.     // ************ 2 ways to getting sorting order *************  
  96.   
  97.     // We can get sort order directly once you done sort by drag & drop  
  98.     $("#tabs ul").bind("sortupdate"function (event, ui) {  
  99.         event.stopPropagation();  
  100.         alert($("#tabs ul").sortable('toArray'));  
  101.     });  
  102.   
  103.     // Another way of getting current sort order of tab using below script  
  104.     $("#showSortable").click(function () {  
  105.         alert($("#tabs ul").sortable('toArray'));  
  106.     });  
  107. </script>  
Here, we have added all the required source code to make it work.
 
Notes 
  • This is a pure jQuery based example of jQuery CRUD operation for Tab.
  • Here, I have tried to make code as much as simple and clean.
  • There are actually many ways to get certain operations done in jQuery but I have chosen some basic and easy ways to understand scripts.
Screens
 
This is the default screen - we are showing one tab already created here. 
 
Dynamic jQuery Tabs - Add, Update, Delete And Sorting
 
The popup for adding a new tab will be shown upon a click of the Add Tab button. 
 
Dynamic jQuery Tabs - Add, Update, Delete And Sorting
 
Here we can see a new tab named "Test Tab" has been added successfully. 
 
Dynamic jQuery Tabs - Add, Update, Delete And Sorting
 
The Remove Tab button will delete the active or selected tab from the tab list.
 
Dynamic jQuery Tabs - Add, Update, Delete And Sorting
 
We ask confirmation from the user whether we would like to go ahead with this delete action. 
 
Dynamic jQuery Tabs - Add, Update, Delete And Sorting
 
On approval, we can see a tab named "Profile" has been deleted successfully. 
 
Dynamic jQuery Tabs - Add, Update, Delete And Sorting
 
Here, I have given another feature to sort the tab like the below screen. Just drag & drop.
 
Dynamic jQuery Tabs - Add, Update, Delete And Sorting
 
Once drag & drop done, it will show you the latest tab order. (The ID attribute is being used here)
 
Dynamic jQuery Tabs - Add, Update, Delete And Sorting
 
Another option to get the latest tab order by clicking the button named "Get Sort Order" 
 
Dynamic jQuery Tabs - Add, Update, Delete And Sorting
 
I hope you liked this article and as a reference, I have attached a single file source code with this article.
 
Many thanks for reading!!