Using jQuery, the answer is simple. Just mark these buttons with a new class called toggle-text and use <span> and <span class="hidden"> to mark the text you want to switch back and forth:
<a class="btn btn-primary toggle-text" data-toggle="collapse" href="#collapseExample"> See <span>more</span><span class="hidden">less</span>... </a>
You can have as many or more <span> tags as you want, and you can order them as you wish, all that matters is whether they are marked with the hidden class.
Now in jQuery you can do:
$('.hidden').removeClass('hidden').hide(); $('.toggle-text').click(function() { $(this).find('span').each(function() { $(this).toggle(); }); });
The first line is the boiler plate bit - jQuery and Bootstrap have somewhat conflicting ideas about hidden ones. It just flips everything that you mark as hidden using the Bootstrap hidden class to hide jQuery, which makes it easier to use the jQuery visibility function after that.
The next bit is important material - it sets a handler for all elements marked with our new toggle-text class - this handler switches the visibility of the child <span> elements of this element when clicked.
See everything in action with this jFiddle - http://jsfiddle.net/w3y421m9/1/
Note: the name toggle-text is completely arbitrary and is used only for the marking buttons for which we want this behavior to be switched.
This is the first time I wrote a question with the intention of answering it. I am interested to know if anyone has a better answer.
For me, this solution looks super simple and has the big plus of saving button text along with HTML for the button, without an extra button specific to CSS or Javascript / jQuery.
jQuery is good in that it does not need to be configured for each button depending on the text you want to display. And you have a lot of flexibility when mixing the text you are doing, and don't want to switch, for example. following equivalents:
<span>Show more...</span><span class=hidden">Show less...</span> Show <span>more</span><span class=hidden">less</span>... Show <span class=hidden">less</span><span>more</span>...
George Hawkins Feb 13 '15 at 13:24 2015-02-13 13:24
source share