How to add a close icon in bootstrap tabs?
Asked Answered
L

3

25

I want to add a close icon in bootstrap tabs and then I can close the tab by click the icon.

I try below but the "X" is displayed not on the same line as tab title.

.close {
    font-size: 20px;
    font-weight: bold;
    line-height: 18px;
    color: #000000;
    text-shadow: 0 1px 0 #ffffff;
    opacity: 0.2;
    filter: alpha(opacity=20);
    text-decoration: none;
    display:inline;
}
.close:hover {
    display:inline;
    color: #000000;
    text-decoration: none;
    opacity: 0.4;
    filter: alpha(opacity=40);
    cursor: pointer;
}

<a id="user-list-tab-li" style="display:inline;" href="#user-list-tab-pane">The tab</a> 
<span class="close">×</span>
Loran answered 7/8, 2013 at 7:3 Comment(0)
G
35

the working fiddle is here

 function registerCloseEvent() {

$(".closeTab").click(function () {

    //there are multiple elements which has .closeTab icon so close the tab whose close icon is clicked
    var tabContentId = $(this).parent().attr("href");
    $(this).parent().parent().remove(); //remove li of tab
    $('#myTab a:last').tab('show'); // Select first tab
    $(tabContentId).remove(); //remove respective tab content

});
 }
Gantline answered 7/8, 2013 at 7:10 Comment(4)
i think its better to use hide(). si it easier to show() it again.Estheresthesia
-1 for fiddle example, where you put <button> inside <a>. Why we don't want this: #6394327Manlike
I would add a check to see if the to-be-removed tab is currently displayed before displaying the last one: if ($(tabContentId).is(":visible"))Lawley
@Manlike what is the alternative if you want the button to appear on the tab as shown in the fiddle, but want to avoid <button> inside <a>? See my related question here: #39403906Consanguineous
D
10

Try to put the span-tag inside the a-tag:

<a id="user-list-tab-li" style="display:inline;" href="#user-list-tab-pane">The tab<span class="close">×</span></a> 

And if you use bootstrap include an icon like this:

<i class="icon-remove"></i>
Drawbar answered 7/8, 2013 at 7:15 Comment(4)
The issue is that even though the close button was clicked, an event is sent to the <a> tag because that is clicked as well.Tocsin
event.preventDefault()should handle this case.Drawbar
yep I know, I'm just saying that there is something to watch out for as well, and just not return false; because that does other things in addition to preventing the default actions.Tocsin
I've used this solution, but event.preventDefault() was not what I needed: to stop the parent <a> tag registering the event I had to use event.stopPropagation().Fluvial
G
0

Small tweaks to Vinod Louis's answer - relative link to the li list and only show a tab if it is the current one closing.

function close_tab (tab_li)
{
    var tabContentId = $(tab_li).parent().attr("href");
    var li_list = $(tab_li).parent().parent().parent();
    $(tab_li).parent().parent().remove(); //remove li of tab
    if ($(tabContentId).is(":visible")) {
        li_list.find("a").eq(0).tab('show'); // Select first tab
    }
    $(tabContentId).remove(); //remove respective tab content
}

Then attach:

$(".closeTab").click(close_tab(this));

Or:

<button class="close closeTab" type="button" onclick="close_tab(this)" >×</button>
Gainless answered 29/5, 2016 at 2:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.