1

I want to create a toggle button where will change button class from class="fa fa-toggle-off" to class="fa fa-toggle-on" when clicked.

<button class="btn btn-default" id="btn" name="btn"><i id="change" class="fa fa-toggle-off"></i></button>

I create the javascript below, however it changed the button style="display:none" instead change its class.

       $(function() {
          $('#btn').click(function(e) {
            e.preventDefault();

            var display = true,
                image   = 'details_close.png';

            if ($('.td1:visible').length == $('.td1').length) { 
              display = false;
              image   = 'details_open.png';
            }

            $('.td1').toggle(display);

            $("#change").toggle(function() 
            {
                    $('#change').removeClass("fa-toggle-off").addClass("fa-toggle-on");
            }, function() {
                    $('#change').removeClass("fa-toggle-on").addClass("fa-toggle-off");
            });
          });
        });
5
  • Are you sure there aren't any other events firing at the same time? Commented Mar 12, 2019 at 10:33
  • Isn’t jQuery’s .toggle() actually toggling the visibility (via CSS’ display attribute) of elements and thus the cause of this behavior? Commented Mar 12, 2019 at 10:36
  • Actually the button is used to toggle the table child row. And i want make the toggle button icon changed when clicked Commented Mar 12, 2019 at 10:37
  • api.jquery.com/toggle-event Commented Mar 12, 2019 at 10:37
  • 1
    In the code given you set up a listener each time a button is clicked; this is obviously wrong. What you actually should use is api.jquery.com/toggleclass Commented Mar 12, 2019 at 10:39

3 Answers 3

2

There you go, I used toggleClass (http://api.jquery.com/toggleclass/) function to toggle the class when you click your button, it'll disabled this class fa-toggle-off and activate this class fa-toggle-on on click (https://api.jquery.com/click/) and vice versa.

$(document).ready(function(){
  $("#btn").click(function() {
     $("#change").toggleClass("fa-toggle-off fa-toggle-on");
  });
});
.fa-toggle-off {
  background-color: #F00;
}

.fa-toggle-on {
  background-color: #0F0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class="btn btn-default" id="btn" name="btn">
  Button <i id="change" class="fa fa-toggle-off">AAA</i>
</button>

Beware, in your code you're checking if #change is clicked, the button got #btn ID attribute. Wish I helped you.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use .toggleClass to add or remove class alternatively

$(document).ready(function() {
  $("#btn").click(function() {
    $("#change").toggleClass("fa-toggle-on");
  });
});
.fa-toggle-off {
  color: red;
}

.fa-toggle-on {
  color: blue;
}

.btn-default {
  display: block;
  width: 200px;
  height: 50px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class="btn btn-default" id="btn" name="btn"><i id="change" class="fa fa-toggle-off">Hello my button</i></button>

Comments

0

You can check if on/off class exist and then can remove existing class & add new class as below code:

$(function() {
    $('#btn').click(function(e) {
        e.preventDefault();

        var display = true,
            image   = 'details_close.png';

        if ($('.td1:visible').length == $('.td1').length) { 
          display = false;
          image   = 'details_open.png';
        }

        $('.td1').toggle(display);

        if ($("#change").hasClass("fa-toggle-off")) 
        {
            $('#change').removeClass("fa-toggle-off").addClass("fa-toggle-on");
        } else {
            $('#change').removeClass("fa-toggle-on").addClass("fa-toggle-off");
        });
    });
});

Hope it helps you.

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.