javascript - How to toggle between 2 icons -
i have toggle between 2 icons based on criteria. how do 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"); } // ... }
do 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'); }});
Comments
Post a Comment