I am using font awesome 'plus' icon on expandable categories list items. When they are in expanded state i want to show a 'minus' sign'
HTML
<ul id="category-tabs">
<li><a href="javascript:void"><i class="fa fa-fw"></i>Category 1</a>
<ul>
<li><a href="javascript:void">item 1</a></li>
<li><a href="javascript:void">item 2</a></li>
<li><a href="javascript:void">item 3</a></li>
</ul>
</li>
</ul>
Jquery
$('#category-tabs li a').click(function(){
$(this).next('ul').slideToggle('500');
});
You can toggle the class of the
i
element within the clicked anchor likethen
Demo: Fiddle
There is another solution you can try by using only the css here is the answer i posted in another post: jQuery Accordion change font awesome icon class on click
//Jquery
JSFiddle
You can change the code by using class definition for the
i
element:Then you can switch the classes rapresenting the plus/minus state using
toggleClass
with multiple classes:Demo: http://jsfiddle.net/Zcn2u/
Generally and simply it works like this:
Simply call jQuery's
toggleClass()
on thei
element contained within youra
element(s) to toggle either the plus and minus icons:Note that this assumes that a class of
fa-plus-circle
is added to youri
element by default.JSFiddle demo.