How to toggle between two icons without buttons using jQuery? Last Updated : 08 Jul, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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).ApproachThe approach is to simply add an “on click” event listener to both of the icons and toggle the CSS classes of both 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, One with the class name of “fa-bars” (Menu icon)One with the class name of “fa-times” (Cross icon)Whenever a user clicks 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. Example: This example shows the implementation of the above-mentioned approach. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width,initial-scale=1.0" /> <!--Font Awesome Icons--> <script src= "https://kit.fontawesome.com/f2c150d561.js" crossorigin="anonymous"> </script> <!--jQuery CDN--> <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity= "sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"> </script> </head> <body> <!--First Icon--> <i class="fas fa-bars" style="font-size: 60px;"></i> <script> $(".fas").click(function () { $(".fas").toggleClass("fa-bars fa-times"); }); </script> </body> </html> Output: Comment More infoAdvertise with us Next Article How to make a Icon Button using jQuery Mobile ? A arpit2205 Follow Improve Article Tags : JQuery jQuery-Questions Similar Reads How to Toggle Between Two Classes in jQuery ? In this article, we will see how to toggle between two classes in jQuery. To make the classes toggle, we will use toggleClass() method. The toggleClass() method is used to toggle or switch the class with the selected class element. Syntax: $(selector).toggleClass(class1 class2) Example 1: In this ex 2 min read How to make a Icon Button using jQuery Mobile ? jQuery Mobile is a web based technology used to make responsive content that can be accessed on all smartphones, tablets and desktops. In this article, we will be making an Icon Button using jQuery Mobile. Approach: First, add jQuery Mobile scripts needed for your project. <link rel="stylesheet" 1 min read How to make an Icon shadow Button using jQuery Mobile ? jQuery Mobile is a web based technology used to make responsive content that can be accessed on all smartphones, tablets and desktops. In this article, we will be making an Icon shadow Button using jQuery Mobile. Approach: First, add jQuery Mobile scripts needed for your project. <link rel="style 1 min read How to make an Icon position Button using jQuery Mobile? jQuery Mobile is a web-based technology for creating responsive content accessible on all smartphones, tablets, and desktops. In this article, we'll show you how to create buttons with various icon positions using jQuery Mobile, enhancing your user interface with flexible and visually appealing butt 2 min read How to create Edit icon using jQuery Mobile ? jQuery Mobile is a web based technology used to make responsive content that can be accessed on all smartphones, tablets and desktops. In this article, we will be making Edit icon using jQuery Mobile. Approach: First, add jQuery Mobile scripts needed for your project. <link rel=âstylesheetâ href= 1 min read How to create a Home icon using jQuery Mobile ? jQuery Mobile is a web-based technology used to make responsive content that can be accessed on all smartphones, tablets, and desktops. In this article, we will be making a Home icon using jQuery Mobile. Approach: First, add jQuery Mobile scripts needed for your project. <link rel="stylesheet" hr 1 min read Like