4
votes

I am using Bootstrap 3 with Jquery. I have a button that contains an icon in a span tag.(using glypicon of bootstrap)

<button id="swapArchived" class="btn btn-default btn-sm showArchived">
       <span class="glyphicon glyphicon-road"></span> Show Archived
</button>

I want to change the icon and text when clicked. Is there any better way other than,

$('#swapArchived').on('click', function () {
    var $el = $(this);
    if($el.hasClass('showArchived'))
    {
      $el.html('<span class="glyphicon glyphicon-fire"></span> Show Incomplete');
    }
    else
    {      
      $el.html('<span class="glyphicon glyphicon-road"></span> Show Archived');
    }
      $el.toggleClass('showArchived');
  });

I was curious whether I can toggle button text and span class at the same time. Trying to avoid writing span on every click.

Thanks-

3
There is a typo in the class name showArchieved and showArchived?Arun P Johny
thanks for informing the typo.Jhankar Mahbub

3 Answers

14
votes

Try

jQuery(function ($) {

    $('#swapArchived').on('click', function () {
        var $el = $(this),
            textNode = this.lastChild;
        $el.find('span').toggleClass('glyphicon-fire glyphicon-road');
        textNode.nodeValue = 'Show ' + ($el.hasClass('showArchieved') ? 'Incomplete' : 'Archived')
        $el.toggleClass('showArchieved');
    });
});

Demo: Fiddle

0
votes

I'd wrap the text in something identifiable first, eg

<button id="swapArchived" class="btn btn-default btn-sm showArchived">
    <span class="glyphicon glyphicon-road"></span>
    <span class="text">Show Archived</span>
</button>

Then simply change the attributes / text required

$('#swapArchived').on('click', function(e) {
    var btn = $(this),
        icon = btn.find('.glyphicon'),
        text = btn.find('.text'),
        toggleClass = 'showArchived';

    if (btn.hasClass(toggleClass)) {
        icon.removeClass('glyphicon-road').addClass('glyphicon-fire');
        text.text('Show Incomplete');
    } else {
        icon.removeClass('glyphicon-fire').addClass('glyphicon-road');
        text.text('Show Archived');
    }
    btn.toggleClass(toggleClass);
});
0
votes

First put the text into another element so it's easier to replace:

<button id="swapArchived" class="btn btn-default btn-sm showArchived">
    <span class="glyphicon glyphicon-road"></span>
    <span>Show Archived</span>
</button>

Then, something like this should work:

$('#footer-top').click(function(){
    var el   = $(this),
        next = el.next(),
        txt  = "Show Incomplete";

    if (/Inc/.test(next.text()))
        var txt = "Show Archived";

    $(this).toggleClass("glyphicon-road")
           .toggleClass("glyphicon-fire");
    next.text(txt);
})

Or, shorter but maybe less readable:

$('#swapArchived').click(function(){
    $(this).toggleClass("glyphicon-road")
           .toggleClass("glyphicon-fire")
           .next().text("Show " + 
               /Inc/.test( $(this).text()) ? "Archived" : "Incomplete");
}

It's not amazingly concise, but it's not bad.