Toggle Bootstrap button text on button click?

后端 未结 5 666
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-19 06:37

I want to toggle the text on a button each time it\'s clicked, e.g. from \"Show more...\" to \"Show less...\".

I have a button that\'s used to expand or collapse a n

5条回答
  •  粉色の甜心
    2020-12-19 07:13

    Thanks for your suggestions. You pointed me in the right direction but I ended up doing it slightly different. My solution uses a bit of CSS to tell what the "hidden" class means and then jQuery toggles this class on all SPAN elements:

    HTML (similar to yours):

    
      Show more...
      
    
    

    CSS:

    .toggle-link .hidden {
      display: none;
    }
    

    Update: You don't need this bit of css if you're using bootstrap, as pointed out by George Hawkins

    jQuery:

    jQuery(".toggle-link").click(function() {
        jQuery(this).find("span").toggleClass("hidden");
        /* Add more logic here */
    });
    

    I'm not saying it's better per se but to me it looks a bit cleaner. Also, you could expand on this method by not hiding a text but changing it's color back and forth, etcetera.

提交回复
热议问题