Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to toggle font awesome icon on click?

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'); }); 

enter image description here

like image 824
Orahmax Avatar asked Apr 24 '14 10:04

Orahmax


People also ask

How do I use Font Awesome icon as button?

In order to do this whilst retaining the <input> element, you must put the Font Awesome glyph outside of the <input> and then simply position it on top. Then simply add some CSS to make the whole button clickable.

How do I display Font Awesome icons?

You can place Font Awesome icons just about anywhere using the CSS Prefix fa and the icon's name. Font Awesome is designed to be used with inline elements (we like the <i> tag for brevity, but using a <span> is more semantically correct). icon If you change the font-size of the icon's container, the icon gets bigger.

How do I display Font Awesome icons in HTML?

Add Icons to HTML We recommend using <i> element with the Font Awesome CSS classes for the style class for the style of icon you want to use and the icon name class with the fa- prefix for the icon you want to use.

How do I add Font Awesome icons to my input fields?

It is as simple as putting Font Awesome icon on any button. The <i> tag and <span> tag are used widely to add icons on the webpages. To add any icons on the webpages, it needs the font-awesome link inside the head section. The font-awesome icon can be placed by using the fa prefix before the icon's name.


2 Answers

You can toggle the class of the i element within the clicked anchor like

<i class="fa fa-plus-circle"></i> 

then

$('#category-tabs li a').click(function(){     $(this).next('ul').slideToggle('500');     $(this).find('i').toggleClass('fa-plus-circle fa-minus-circle') }); 

Demo: Fiddle

like image 182
Arun P Johny Avatar answered Sep 21 '22 04:09

Arun P Johny


Simply call jQuery's toggleClass() on the i element contained within your a element(s) to toggle either the plus and minus icons:

...click(function() {     $(this).find('i').toggleClass('fa-minus-circle fa-plus-circle'); }); 

Note that this assumes that a class of fa-plus-circle is added to your i element by default.

JSFiddle demo.

like image 25
James Donnelly Avatar answered Sep 21 '22 04:09

James Donnelly