How To Add and Remove Tabs in Bootstrap 3 Dynamically?

Published on 27 Jul 2013
Bootstrap JavaScript jQuery Programming Web Development
How To Add and Remove Tabs in Bootstrap 3 Dynamically?

The code is compatible with Bootable 3.x

Earlier today, I was working on a web application with Bootstrap and I was required to allow a user to add and remove tabs. Unfortunately Bootstrap doesn’t give you the close button on a tab out-of-the-box. So I quickly went to the Bootstrap’s Issues page on GitHub where I stumbled upon the very problem I was facing.

A Mark, one of the maintainers of the project was of the opinion:

I don’t think we’ll be adding this as native functionality, but feel free to file an issue later if you wish to add whatever solution you come up with to the wiki for forks/extensions/etc.

I was like wow! Now, everyone was proposing their way of doing it. So, I came up with my solution thanks to this guy who proposed this solution. He just gave an heads-up and that was all I needed, then I sat and wrote the three features I needed i.e. Add, Remove and renumber the tabs (if in case one is removed from the middle. and yeah, the first tab cannot be removed).

 

The Code

The following code assumes Bootstrap and jQuery scripts and styles are loaded.

/**
*Let's define a variable that acts similar to the 'On Air' light. When
*its active (true in this case), we will just return from the doSignup function
* silently or inform the user via a jquery-ui dialog or something like that.
*/
var isProcessing = false;
function doSignup() {
if (isProcessing) {
/*
*This won't go past this condition while
*isProcessing is true.
*You could even display a message.
**/
return;
}
/**
*set isProcessing to true right before the $.post (or anyother method
*of the sort like $.ajax, $.get) is called
*/
isProcessing = true;
$.post('http://awsomesite/ajax.php', $('#signupForm').serialize(),
function (resp) {
isProcessing = false;
if (resp.status == 'OK') {
alert("Success message");
} else {
alert("duh! Signup failed");
}
}, 'JSON');
}

 

The Demo

Check out the demo here.

Please like and share the post if you enjoyed it and if it helped you.

Comments

Image of Steven Bengtson Steven Bengtson says

This is great except for a couple of things:
1. If you add 2 tabs so you have tabs 1, 2, and 3. Then remove tab 2, then add another tab, the content ids will get duplicated (you will have 2 contents with the id of page3.

2. I would put this in a namespace so you don't have to use globals (pageNum)
(function( MyPageTabs, $, undefined ) {
MyPageTabs.init = function()
{
// do your initialization here (bind events etc)
};

var pageNum = 1;

function addTab()
{
// ...
}

function removeTab()
{
// ...
}

function renumberTabs()
{
// ....
}
}(window.MyPageTabs = window.MyPageTabs || {}, jQuery));

// On Document Load Ready
$(function() {
MyPageTabs.init();
});

3. I would also add the elements using jQuery rather than strings:
var newRemoveIcon = $(').addClass('close')...;
var newTabLink = $('').attr('href', '...')...append(newRemoveIcon);
var newTab = $(').append(newTabLink);

Image of chouxpastry2002 chouxpastry2002 says

This is buggy code and seems like major snippets to do this function over internet are having same bug..

If you remove intermediate tab after generating it, you will have one tab completely inaccessible.