0

I have this below script

  $.fn.SoundToggle = function() {
            var sound_staus = $('#soundstatus').data('status');
            if (sound_staus === 'mute')
            {
                $('#soundstatus').data('status', 'unmute');//ui-icon ui-icon-volume-off
                $("#soundstatus span").removeClass("ui-icon ui-icon-volume-off").addClass("ui-icon ui-icon-volume-on");
            } else
            {
                $('#soundstatus').data('status', 'mute');
                $("#soundstatus span").removeClass("ui-icon ui-icon-volume-on").addClass("ui-icon ui-icon-volume-off");
            }
        }

And html

<a class="button"  title="Sound Mute/Unmute" data-status="mute" onClick="$(this).SoundToggle();" id="soundstatus"> 
                    <span class="ui-icon ui-icon-volume-on"></span>
</a>

I want to toggle jquery ui Speaker Mute & unmute two icons on click.

But my above code is not showing output properly, instead of removing previous its showing two icons at once like this image:

enter image description here

Please help me why my code is not working fine.

12
  • 4
    bacause it's 3 min ago .. wait for few more . Commented Oct 8, 2013 at 18:36
  • 1
    Why are you removing and adding the same class ui-icon? Commented Oct 8, 2013 at 18:37
  • 1
    @Barmar or better yet, this Commented Oct 8, 2013 at 18:41
  • 1
    @rani what he's saying is there's no point in removing/adding ui-icon because it's needed for both versions of it. Commented Oct 8, 2013 at 18:43
  • 1
    Because in your example, your initial value of status is UNMUTE Commented Oct 8, 2013 at 18:52

3 Answers 3

2

As Jhonatas Kleinkauff said: you have your status and icons mixed in your initial code. On the first click, the status would be mute, so you would try to remove the class ui-icon-volume-off, which is not there, so in fact, no class gets removed. Afterwards you are adding ui-icon-volume-on and end up having both classes set at the same time, so both icons are shown.

You are doing it right in your fiddle though (status is unmute, so sound is on and the class should be ui-icon-volume-on, which it is).

To fix the code in your question, you would have to either switch the initial status to unmute as you did in the fiddle:

<a class="button" title="Sound Mute/Unmute" data-status="unmute" onClick="$(this).SoundToggle();" id="soundstatus"> 
    <span class="ui-icon ui-icon-volume-on"></span>
</a>

or set the other HTML class:

<a class="button" title="Sound Mute/Unmute" data-status="mute" onClick="$(this).SoundToggle();" id="soundstatus"> 
    <span class="ui-icon ui-icon-volume-off"></span>
</a>

You might want to look into jQuery .toggleClass function which has a nice second parameter, a boolean switch to indicate if you want to add or remove the class. You could then do something like this:

var $soundIcon = $('#soundstatus span');
var soundShouldBeTurnedOn = currentStatus === "mute";
$soundIcon.toggleClass("ui-icon-volume-on", soundShouldBeTurnedOn)
          .toggleClass("ui-icon-volume-off", !soundShouldBeTurnedOn);
Sign up to request clarification or add additional context in comments.

2 Comments

But it is working in your fiddle, just get the code from there. The way I understand it: if the data-status is mute, the icon should be off. This is different in your HTML but correct in your JS.
yeah , you are right. I tested my fiddle also. But its again displaying three icons . I am getting mad
1
 $.fn.SoundToggle = function() {
            var sound_staus = $('#soundstatus').data('status');
            if (sound_staus === 'mute')
            {
                $('#soundstatus').data('status', 'unmute');//ui-icon ui-icon-volume-off
                $("#soundstatus span").removeClass("ui-icon ui-icon-volume-on").addClass("ui-icon ui-icon-volume-off");
            } else
            {
                $('#soundstatus').data('status', 'mute');
                $("#soundstatus span").removeClass("ui-icon ui-icon-volume-off").addClass("ui-icon ui-icon-volume-on");
            }
        }

On the original code:

 if (sound_staus === 'mute')
            {
                ...
                $("#soundstatus span").removeClass("ui-icon ui-icon-volume-off").addClass("ui-icon ui-icon-volume-on");
            }..

But, the initial value of status IS MUTE AND the class is ui-icon ui-icon-volume-on

So, why when mute, you try to remove ui-icon ui-icon-volume-off instead on?

3 Comments

i'm having a hard time figuring out what you've changed.
He swapped on and off.
Your answer is displaying just opposite icon of my wish.Which is wrong. Just now i tested my above question code. Here is the fiddle of my above question jsfiddle.net/TrxHx/4 . Its working fine, but why its not working fine in my app?
0

I solved it by providing id to the icon div.

I think because of the huge code, it was showing multiple icons, by confilcting with several span.

I thought, id should be unique hence i added a id attribute i.e id="volumeicondId"

<a id="soundstatus" class="button"  title="Sound Mute/Unmute" data-status="unmute" onClick="$(this).SoundToggle();" > 
                    <span id="volumeicondId" class="ui-icon ui-icon-volume-on"></span>

And js function

  $.fn.SoundToggle = function() {
            var sound_staus = $('#soundstatus').data('status');
            if (sound_staus === 'unmute')
            {
                $('#soundstatus').data('status', 'mute');//ui-icon ui-icon-volume-off
                $("#volumeicondId").removeClass("ui-icon ui-icon-volume-on").addClass("ui-icon ui-icon-volume-off");
            } else
            {
                $('#soundstatus').data('status', 'unmute');
                $("#volumeicondId").removeClass("ui-icon ui-icon-volume-off").addClass("ui-icon ui-icon-volume-on");
            }
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.