Skip to content
Related Articles

Related Articles

How to toggle between two icons without buttons using boot-strap and jQuery ?

Improve Article
Save Article
  • Last Updated : 18 Jan, 2021
Improve Article
Save Article

Given an HTML document with two icons, the task is to alternatively display the icons each time when a user clicks on the icon. This task can be easily accomplished with the help of jQuery (A JavaScript library).

Approach: The approach is to simply add an “on click” event listener to both of the icons and toggle the CSS classes of both the icons using jQuery. These classes are ultimately responsible for displaying a specific icon from the icon library (Fontawesome, in this case). Here, we have used two icons, 

  1. One with the class name of “fa-bars” (Menu icon)
  2. One with the class name of “fa-times” (Cross icon)

Whenever a user will click on the menu icon, its “fa-bars” class will be toggled to “fa-times”, thus the cross icon will be then displayed and vice versa is also true. 



<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8" />
    <meta name="viewport" content=
        "width=device-width,initial-scale=1.0" />
    <!--Bootstrap CSS CDN-->
    <link rel="stylesheet" href=
        crossorigin="anonymous" />
    <!--Font Awesome Icons-->
    <!--jQuery CDN-->
    <!--First Icon-->
    <i class="fas fa-bars m-5" style="font-size: 60px;"></i>
        $(".fas").click(function () {
            $(".fas").toggleClass("fa-bars fa-times");


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!