简体   繁体   中英

How to toggle between 2 icons

I have to toggle between two icons based on some criteria. How do I do this in jQuery?

<a href='' id="home" onclick="myfunction(this)" data-toggle="toggle">
    <span class="fa fa-train"></span>
</a>
myfunction($this) {
    // ...

    if ($($this).hasClass("fa fa-train")) {
        $($this).removeClass("fa fa-train");
        $($this).addClass("fa fa-car");
    }
    else {
        $($this).addClass("fa fa-car");
        $($this).addClass("fa fa-train");
    }

    // ...
}

clicked anchor element do not have class added, its child span does :

function myfunction($this) {
  $($this).find('span').toggleClass('fa-train fa-car');
}

Do something like this

 $('a#home').on('click', function(){
   var childSpan = $(this).find('span');
   if(childSpan.hasClass('fa-train')){
     childSpan.removeClass('fa-train');
     childSpan.addClass('fa-car');
   }
   else if(childSpan.hasClass('fa-car')){
    childSpan.removeClass('fa-car');
    childSpan.addClass('fa-train');
   }});

you are using this which will check the a tag if has class . while you should check in the span next to a tag .you should use like this

myfunction($this) {
    // ...

    if ($($this).next('span').hasClass("fa fa-train")) {
        $($this).next('span').removeClass("fa fa-train");
        $($this).next('span').addClass("fa fa-car");
    }
    else {
        $($this).next('span').addClass("fa fa-car");
        $($this).next('span').addClass("fa fa-train");
    }

    // ...
}

With a condition in one line:

$($this)
  .addClass((condition=(Math.random() > .5)) ? 'fa-car' : 'fa-train')
  .removeClass(condition ? 'fa-train': 'fa-car')

Note: This will not work if you "use strict" and of course your code will be less readable.

Thanks everyone, yes i had to search for 'span' and add toggle class. hasClass is also good way to check beforehand that the class exist or not.

This is a concept that I came with which is a bit logical. Instead of using complicated language, what this does is it hides the add button and makes a times button while showing the message that was hidden. It is fairly simple JQuery.

 $(document).ready(function() { $("#addCross").click(function() { document.getElementById("addCross").style.display = "none"; document.getElementById("addAdd").style.display = "inline-block"; document.getElementById("hello").style.display = "block"; }); $("#addAdd").click(function() { document.getElementById("addCross").style.display = "inline-block"; document.getElementById("addAdd").style.display = "none"; document.getElementById("hello").style.display = "none"; }); });
 /* Unoptional CSS Just to make the buttons look better in the snippet. */ button { background: transparent; border: none; } button:focus { outline: none; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://kit.fontawesome.com/9b271ce18a.js" crossorigin="anonymous"></script> <div> <label>Press the Button: </label> <button id="addCross"><i class="fas fa-plus"></i> <button id="addAdd" style="display: none;"><i class="fas fa-times"></i></button> <div style="display: none;" id="hello"> <p>Hello. I was hidden by the Magic of JQuery.</p> <label>This design also makes a cool rotating add sign if you look closely. Ha Ha!!</label> </div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM