Dynamically adding a tab in bootstrap doesn't show tab-pane content after navigating away and back











up vote
0
down vote

favorite












Ive seen a lot of similar questions, but nothing that is quite like this (that I can find).



This is in a rails 5 app, bootstrap-sass 3.3, jquery 1.12.4.



$("#opendata").on("click", function(){
$("<div class='tab-pane active' id='summary'>hi</div>").appendTo("#tab-content");
$("<li class='active' data-toggle='tab'><a href='summary' aria-expanded='true'>Summary</a></li>").appendTo("#tabs")
})


I have 2 tabs that are loaded with the page, and need to add other tabs dynamically. The above code works - the tab is added, and the tab-pane contents are displayed. However if I navigate away from that tab, and navigate back (the tab itself will go active) the tab-pane is showing the contents of the previous tab.



I've narrowed the issue down to the dynamic tab-pane not getting an 'active' class put on it. I've tried to do this via console as well, and it doesn't work either.



The main solution I saw for dynamic tabs was to add something like this



jQuery(function($){
$("tabs").on("click", "a", function(e){
e.preventDefault();
$(this).tab('show');
})
})


But to no avail. It seems like the added tab-pane just doesn't respond to anything, console or otherwise. Am I missing something or going about this the wrong way? The tab gets added fine, just when navigating away and back that it doesn't respond.










share|improve this question







New contributor




user10567361 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Have you tried showing the tabs by directly calling it's id? $('#summary').tab('show') . $("tabs") seems like it might be the wrong selector.
    – I. R. R.
    Nov 21 at 23:03












  • I did - for both the tabs that are loaded with the page, as well as the dynamically added tabs. Didn't work for any of them.
    – user10567361
    Nov 22 at 15:46















up vote
0
down vote

favorite












Ive seen a lot of similar questions, but nothing that is quite like this (that I can find).



This is in a rails 5 app, bootstrap-sass 3.3, jquery 1.12.4.



$("#opendata").on("click", function(){
$("<div class='tab-pane active' id='summary'>hi</div>").appendTo("#tab-content");
$("<li class='active' data-toggle='tab'><a href='summary' aria-expanded='true'>Summary</a></li>").appendTo("#tabs")
})


I have 2 tabs that are loaded with the page, and need to add other tabs dynamically. The above code works - the tab is added, and the tab-pane contents are displayed. However if I navigate away from that tab, and navigate back (the tab itself will go active) the tab-pane is showing the contents of the previous tab.



I've narrowed the issue down to the dynamic tab-pane not getting an 'active' class put on it. I've tried to do this via console as well, and it doesn't work either.



The main solution I saw for dynamic tabs was to add something like this



jQuery(function($){
$("tabs").on("click", "a", function(e){
e.preventDefault();
$(this).tab('show');
})
})


But to no avail. It seems like the added tab-pane just doesn't respond to anything, console or otherwise. Am I missing something or going about this the wrong way? The tab gets added fine, just when navigating away and back that it doesn't respond.










share|improve this question







New contributor




user10567361 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Have you tried showing the tabs by directly calling it's id? $('#summary').tab('show') . $("tabs") seems like it might be the wrong selector.
    – I. R. R.
    Nov 21 at 23:03












  • I did - for both the tabs that are loaded with the page, as well as the dynamically added tabs. Didn't work for any of them.
    – user10567361
    Nov 22 at 15:46













up vote
0
down vote

favorite









up vote
0
down vote

favorite











Ive seen a lot of similar questions, but nothing that is quite like this (that I can find).



This is in a rails 5 app, bootstrap-sass 3.3, jquery 1.12.4.



$("#opendata").on("click", function(){
$("<div class='tab-pane active' id='summary'>hi</div>").appendTo("#tab-content");
$("<li class='active' data-toggle='tab'><a href='summary' aria-expanded='true'>Summary</a></li>").appendTo("#tabs")
})


I have 2 tabs that are loaded with the page, and need to add other tabs dynamically. The above code works - the tab is added, and the tab-pane contents are displayed. However if I navigate away from that tab, and navigate back (the tab itself will go active) the tab-pane is showing the contents of the previous tab.



I've narrowed the issue down to the dynamic tab-pane not getting an 'active' class put on it. I've tried to do this via console as well, and it doesn't work either.



The main solution I saw for dynamic tabs was to add something like this



jQuery(function($){
$("tabs").on("click", "a", function(e){
e.preventDefault();
$(this).tab('show');
})
})


But to no avail. It seems like the added tab-pane just doesn't respond to anything, console or otherwise. Am I missing something or going about this the wrong way? The tab gets added fine, just when navigating away and back that it doesn't respond.










share|improve this question







New contributor




user10567361 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











Ive seen a lot of similar questions, but nothing that is quite like this (that I can find).



This is in a rails 5 app, bootstrap-sass 3.3, jquery 1.12.4.



$("#opendata").on("click", function(){
$("<div class='tab-pane active' id='summary'>hi</div>").appendTo("#tab-content");
$("<li class='active' data-toggle='tab'><a href='summary' aria-expanded='true'>Summary</a></li>").appendTo("#tabs")
})


I have 2 tabs that are loaded with the page, and need to add other tabs dynamically. The above code works - the tab is added, and the tab-pane contents are displayed. However if I navigate away from that tab, and navigate back (the tab itself will go active) the tab-pane is showing the contents of the previous tab.



I've narrowed the issue down to the dynamic tab-pane not getting an 'active' class put on it. I've tried to do this via console as well, and it doesn't work either.



The main solution I saw for dynamic tabs was to add something like this



jQuery(function($){
$("tabs").on("click", "a", function(e){
e.preventDefault();
$(this).tab('show');
})
})


But to no avail. It seems like the added tab-pane just doesn't respond to anything, console or otherwise. Am I missing something or going about this the wrong way? The tab gets added fine, just when navigating away and back that it doesn't respond.







javascript jquery twitter-bootstrap-3






share|improve this question







New contributor




user10567361 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




user10567361 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




user10567361 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 21 at 20:05









user10567361

1




1




New contributor




user10567361 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





user10567361 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






user10567361 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • Have you tried showing the tabs by directly calling it's id? $('#summary').tab('show') . $("tabs") seems like it might be the wrong selector.
    – I. R. R.
    Nov 21 at 23:03












  • I did - for both the tabs that are loaded with the page, as well as the dynamically added tabs. Didn't work for any of them.
    – user10567361
    Nov 22 at 15:46


















  • Have you tried showing the tabs by directly calling it's id? $('#summary').tab('show') . $("tabs") seems like it might be the wrong selector.
    – I. R. R.
    Nov 21 at 23:03












  • I did - for both the tabs that are loaded with the page, as well as the dynamically added tabs. Didn't work for any of them.
    – user10567361
    Nov 22 at 15:46
















Have you tried showing the tabs by directly calling it's id? $('#summary').tab('show') . $("tabs") seems like it might be the wrong selector.
– I. R. R.
Nov 21 at 23:03






Have you tried showing the tabs by directly calling it's id? $('#summary').tab('show') . $("tabs") seems like it might be the wrong selector.
– I. R. R.
Nov 21 at 23:03














I did - for both the tabs that are loaded with the page, as well as the dynamically added tabs. Didn't work for any of them.
– user10567361
Nov 22 at 15:46




I did - for both the tabs that are loaded with the page, as well as the dynamically added tabs. Didn't work for any of them.
– user10567361
Nov 22 at 15:46












1 Answer
1






active

oldest

votes

















up vote
0
down vote













so chalk this one up to looking at the same code for too long on my part. The solution is:



$("<li class='active' data-toggle='tab'><a href='#summary' aria-expanded='true'>Summary</a></li>").appendTo("#tabs")


Notice the '#' in front of the href that I forgot in the first post.






share|improve this answer








New contributor




user10567361 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });






    user10567361 is a new contributor. Be nice, and check out our Code of Conduct.










     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419727%2fdynamically-adding-a-tab-in-bootstrap-doesnt-show-tab-pane-content-after-naviga%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    so chalk this one up to looking at the same code for too long on my part. The solution is:



    $("<li class='active' data-toggle='tab'><a href='#summary' aria-expanded='true'>Summary</a></li>").appendTo("#tabs")


    Notice the '#' in front of the href that I forgot in the first post.






    share|improve this answer








    New contributor




    user10567361 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      0
      down vote













      so chalk this one up to looking at the same code for too long on my part. The solution is:



      $("<li class='active' data-toggle='tab'><a href='#summary' aria-expanded='true'>Summary</a></li>").appendTo("#tabs")


      Notice the '#' in front of the href that I forgot in the first post.






      share|improve this answer








      New contributor




      user10567361 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















        up vote
        0
        down vote










        up vote
        0
        down vote









        so chalk this one up to looking at the same code for too long on my part. The solution is:



        $("<li class='active' data-toggle='tab'><a href='#summary' aria-expanded='true'>Summary</a></li>").appendTo("#tabs")


        Notice the '#' in front of the href that I forgot in the first post.






        share|improve this answer








        New contributor




        user10567361 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        so chalk this one up to looking at the same code for too long on my part. The solution is:



        $("<li class='active' data-toggle='tab'><a href='#summary' aria-expanded='true'>Summary</a></li>").appendTo("#tabs")


        Notice the '#' in front of the href that I forgot in the first post.







        share|improve this answer








        New contributor




        user10567361 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        share|improve this answer



        share|improve this answer






        New contributor




        user10567361 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        answered 2 days ago









        user10567361

        1




        1




        New contributor




        user10567361 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.





        New contributor





        user10567361 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        user10567361 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






















            user10567361 is a new contributor. Be nice, and check out our Code of Conduct.










             

            draft saved


            draft discarded


















            user10567361 is a new contributor. Be nice, and check out our Code of Conduct.













            user10567361 is a new contributor. Be nice, and check out our Code of Conduct.












            user10567361 is a new contributor. Be nice, and check out our Code of Conduct.















             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419727%2fdynamically-adding-a-tab-in-bootstrap-doesnt-show-tab-pane-content-after-naviga%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            What visual should I use to simply compare current year value vs last year in Power BI desktop

            How to ignore python UserWarning in pytest?

            Alexandru Averescu