0

First, let me clear you all one thing that i have already searched a lot but couldn't find the answer which will solve my problem. I have a button which when i click it's icon changes and some functions called than when i again click on that button it's icon must be revert and same functions called again.

I have this button:

<button type="button" class="btn" id="voice_icon" title="microphone">
<i style="font-size: 24px;" class="fa fa-microphone fa-2x voice_control" id="voice_control"></i>
<small class="toolbar-label mic-btn" id="mic">Mic.</small>
</button>

while clicking on that button these functions and classes should be called:

$('#voice_icon').click(function () {
        voiceAtion.voice();
        test.start();
        $('#voice_control').addClass("fa-microphone-slash").removeClass("fa-microphone");
        $('#mic').html("<i class='fa fa-spinner fa-pulse fa-1x fa-fw'></i>");
    });

And when i again click on that icon these icons and classes should be called again :

test.abort();
$('#voice_control').addClass("fa-microphone").removeClass("fa-microphone-slash");
$('#mic').html("Mic.");

$('#voice_icon').click(function() {
  voiceAtion.voice();
  test.start();
  $('#voice_control').addClass("fa-microphone-slash").removeClass("fa-microphone");
  $('#mic').html("<i class='fa fa-spinner fa-pulse fa-1x fa-fw'></i>");
});

test.abort();
$('#voice_control').addClass("fa-microphone").removeClass("fa-microphone-slash");
$('#mic').html("Mic.");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button type="button" class="btn" id="voice_icon" title="microphone">
<i style="font-size: 24px;" class="fa fa-microphone fa-2x voice_control" id="voice_control"></i>
<small class="toolbar-label mic-btn" id="mic">Mic.</small>
</button>

4 Answers 4

4

Try something like this first time click goes to else and for second click it goes to if. so on..

$('#voice_icon').click(function() {
  var clicks = $(this).data('clicks');
  if (clicks) {
    //first code
  } else {
     //second code
  }
  $(this).data("clicks", !clicks);
});
Sign up to request clarification or add additional context in comments.

5 Comments

It's not working in the first time but working fine when i again clicks the button
It will works .. in first click it goes to else condition. check fiddle : jsfiddle.net/p28ppsjb
Initially var clicks takes undefined value so it will goes to else or you can swap your first and second code
what's the meaning of $(this).data("clicks", !clicks); this line?
In your case $(this).data("clicks", !clicks); !clicks which is opposite value stores in to the clicks so every time you get only true and false which is exactly you required in this case
2

Instead of addClass/removeClass use toggleClass.

$('#voice_control').toggleClass("fa-microphone-slash fa-microphone");

3 Comments

Only partial correct, You missed test.abort(); and $('#mic').html("Mic.");
@Satpal: if you think so, you can add your complete solution, or write why this one should be just partial.
what about the functions?
0

use hasClass for check which class to remove and add

$(document).ready(function() {
  $('#voice_icon').click(function() {
    if ($('#voice_control').hasClass("fa-microphone")) {

      $('#voice_control').addClass("fa-microphone-slash").removeClass("fa-microphone");
      $('#mic').html("<i class='fa fa-spinner fa-pulse fa-1x fa-fw'></i>");
    } else {
      $('#voice_control').addClass("fa-microphone").removeClass("fa-microphone-slash");
      $('#mic').html("Mic.");
    }

  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button type="button" class="btn" id="voice_icon" title="microphone">
<i style="font-size: 24px;" class="fa fa-microphone fa-2x voice_control" id="voice_control"></i>
<small class="toolbar-label mic-btn" id="mic">Mic.</small>
</button>

Comments

0
   // You can also use global variable and check for true false:


   <button type="button" class="btn" id="voice_icon" title="microphone">
    <i style="font-size: 24px;" class="fa fa-microphone fa-2x voice_control" id="voice_control"></i>
    <small class="toolbar-label mic-btn" id="mic">Mic.</small>
</button>

    <script>
        var x = true;
        $('#voice_icon').click(function () {
            if(x){
               $('#voice_control').removeClass("fa-microphone").addClass("fa-microphone-slash")
               x = false;
           }
           else{
               $('#voice_control').addClass("fa-microphone").removeClass("fa-microphone-slash")
               x = true;
           }
            $('#mic').html("<i class='fa fa-spinner fa-pulse fa-1x fa-fw'></i>");
        });
    </script>

Comments

Your Answer

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