show dynamically added navlinks when added in bootstrap navbar

I am new to Bootstrap, and can't seem to figure this out, i have navbar being used as javascript tab functionality of Bootstrap, which can have dynamically added and removeable nav-links, i have used images rather than default nav-links, my question is when i add the dynamic nav-link, it should become the active class and show its relevant content, at the moment i have to click to make it active, and if i remove any nav-link, the content remains same, is there a way to achieve this function

The html is:

<ul id="nav-tabs" data-tabs="tabs" >
          <li class="test" ><img src="assets/img/button_home_selected3.png" class="hover" width="83" /><span>Home</span></a> </li>
</ul>

The tabs are added when this button is clicked:

<a href="#" class="plus" title="Click to add Tabs" ><img src="assets/img/icon_plus.png"/></a>

The li are added using

var counter = 1;    
    $('.plus').click(function(e) {
        e.preventDefault();
        var li_count = $('#nav-tabs').find("li.test").length;
        if (li_count <= 3)
            if(counter <= 3){
                $('#nav-tabs').append('<li class="test" ><img src="assets/img/button_home_selected3.png" class="hover" width="83" /><span>Home</span></a><button type="button" class="close">&times;</button></div></a></li>');
                } else { alert("Only 3 Tabs Allowed!")};

The content of tabs are added similarly later;

The active class in tabs is toggled using

$("#nav-tabs").on("click", "a", function(e) {
        e.preventDefault();
        $(this).tab('show');
        $('li.test').each(function() {
            if($(this).hasClass('active'))
            {
                //Active class is applied
                $(this).children().children().closest("img").attr("src", "assets/img/button_home_selected3.png");
                $(this).find("button").show();
            }
            else
            {
                $(this).children().children().closest("img").attr("src", "assets/img/button_home_plain2.png");
                $(this).find("button").hide();
            }


        });

The li are closed using button close in the new nav-links as:

$('.close').click(function(e) {
    e.preventDefault();
    var panelId = $(this).closest("li").remove().attr("aria-controls");
    $("#tab" + panelId).remove();
    $("#nav-tabs").children("li").last().addClass("active");

    if(counter <= 1){
        counter = 1;
        }else if (counter > 1) {
            counter--;
        }
        return false;
})

Answers:

Answer

It doesn't look like you're using the standard Bootstrap nav/nav-tabs markup. If I were you I would simplify adding new tabs and tab content like this...

Have a single function that creates the new tab, tab content and then makes it active. You can use the tab('show') method to activate the newly created tab:

$('#btnAdd').click(function (e) {
    var nextTab = $('#tabs li').size()+1;

    // create the tab
    $('<li><a href="#tab'+nextTab+'" data-toggle="tab">Tab '+nextTab+'</a></li>').appendTo('#tabs');

    // create the tab content
    $('<div class="tab-pane" id="tab'+nextTab+'">tab' +nextTab+' content</div>').appendTo('.tab-content');

    // make the new tab active
    $('#tabs a:last').tab('show');
});

Dynamic Bootstrap Tabs Demo

Then you just need to customize it a little for your images.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.