javascript - Font-awesome class fa is not being added with JQuery - Stack Overflow

I have a button where I want the HTML and font-awesome icon to change on an odd number of clicks and re

I have a button where I want the HTML and font-awesome icon to change on an odd number of clicks and revert back on an even number of clicks.

For example, I have a button named mute. If a user clicks on the button, I want the html to change to unmute and the font-awesome class to be replaced. If the user clicks on the button again I want the button to revert back to mute and so on.

This code should work and it does change the URL and the class. But it doesn't add the font-awesome initialization class fa.

When I inspect the element, I get this

<button id="mute" class="btn btn-primary fa-volume-off"> Mute</button>

With JQuery's addClass method you can add multiple classes by separating them with a space. Based on this, the element should be this

<button id="mute" class="btn btn-primary fa fa-volume-off"> Mute</button>

Here is my code

var clicks = 0;
    $("#mute").click(function() {
            clicks += 1;
            if (clicks % 2 === 1) {
                    $(this).html(" Un-mute");
                    $(this).addClass("fa fa-volume-up");
                    $(this).removeClass("fa fa-volume-off");
                    apiswf.rdio_setVolume(0);
            }
            else {
                $(this).html(" Mute");
                $(this).addClass("fa fa-volume-off");
                $(this).removeClass("fa fa-volume-up");
                apiswf.rdio_setVolume(100);
            }
    });

I do not understand why the fa class does not get added.

I have a button where I want the HTML and font-awesome icon to change on an odd number of clicks and revert back on an even number of clicks.

For example, I have a button named mute. If a user clicks on the button, I want the html to change to unmute and the font-awesome class to be replaced. If the user clicks on the button again I want the button to revert back to mute and so on.

This code should work and it does change the URL and the class. But it doesn't add the font-awesome initialization class fa.

When I inspect the element, I get this

<button id="mute" class="btn btn-primary fa-volume-off"> Mute</button>

With JQuery's addClass method you can add multiple classes by separating them with a space. Based on this, the element should be this

<button id="mute" class="btn btn-primary fa fa-volume-off"> Mute</button>

Here is my code

var clicks = 0;
    $("#mute").click(function() {
            clicks += 1;
            if (clicks % 2 === 1) {
                    $(this).html(" Un-mute");
                    $(this).addClass("fa fa-volume-up");
                    $(this).removeClass("fa fa-volume-off");
                    apiswf.rdio_setVolume(0);
            }
            else {
                $(this).html(" Mute");
                $(this).addClass("fa fa-volume-off");
                $(this).removeClass("fa fa-volume-up");
                apiswf.rdio_setVolume(100);
            }
    });

I do not understand why the fa class does not get added.

Share Improve this question asked Jun 1, 2015 at 18:33 Richard HamiltonRichard Hamilton 26.5k11 gold badges65 silver badges88 bronze badges 2
  • maybe not even add/remove the fa class at all? – Daemedeor Commented Jun 1, 2015 at 18:37
  • Do you really need to count clicks? Wouldn't it be simpler to just call toggleClass on both classes on every click? – Avner Shahar-Kashtan Commented Jun 2, 2015 at 5:45
Add a ment  | 

3 Answers 3

Reset to default 5

The problem is you removing fa class with removeClass method immediately after adding it . Change to this :

$(this).removeClass("fa-volume-off");

Remove the class and then add the class. Swap the position of the two methods in both muting or unmuting

You are removeClass with fa in it after addClass

jsFiddle

var clicks = 0;
$("#mute").click(function() {
    clicks += 1;
    if (clicks % 2 === 1) {
        $(this).html(" Un-mute");
        $(this).removeClass("fa fa-volume-off");
        $(this).addClass("fa fa-volume-up");
        apiswf.rdio_setVolume(0);
    }
    else {
        $(this).html(" Mute");
        $(this).removeClass("fa fa-volume-up");
        $(this).addClass("fa fa-volume-off");
        apiswf.rdio_setVolume(100);
    }
});

Also, the removeClass both have fa even though you add fa every addClass... So should decide how you actually want to handle the fa.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744745766a4591286.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信