How to Use jQuery $(this) in Event ? Last Updated : 28 Apr, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report The scope of this keyword changes according to the scope of the object. The $(this) keyword will refer to the selected element to which the event is attached when used in an event. In this article, we will see the implementation of $(this) with an event in different conditions. Syntax:$('element_selector').on('event_name', callback_function{ $(this) // refers to the selected DOM element});Example 1: In the below code example, we will see the basic implementation of the $(this) keyword in an event. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title> Using $(this) with an event </title> </head> <body> <center> <h2 class="headings">GeeksforGeeks</h2> <h3 class="heading"> Click the below button to style the whole document using the $(this) inside event. </h3> <button id="myBtn"> Click to Style Document </button> </center> <!-- jQuery CDN Link --> <script src= "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js" integrity= "sha512-v2CJ7UaYy4JwqLDIrZUI/4hqeoQieOmAZNXBeQyjo21dadnwR+8ZaIJVT8EE2iyI61OV8e6M8PP2/4hpQINQ/g==" crossorigin="anonymous" referrerpolicy="no-referrer"> </script> <!-- jQuery script --> <script> $(document).ready(()=>{ $('#myBtn').on('click', function(){ $(this). text('Document styled and text changed using $(this)') $(this).prevAll().css('color', 'green'); $(this).css({ 'background': 'green', 'color': '#fff', 'border': 'none', 'padding': '10px', 'border-radius': '8px' }); }); }); </script> </body> </html> Output: Example 2: The below example implements the $(this) inside a navbar to represent the active link on click to it and change text according to the current page using. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title> Using $(this) with an event </title> <style> .unorderedList{ display: flex; align-items: center; justify-content: center; list-style: none; background-color: #ccc; } .listItem{ padding: 10px 30px; } .heading{ color: green; } .link{ color: #000; text-decoration: none; } .activeLink{ background-color: green; color: #fff; } .activeLink > a.link{ color: #fff; } </style> </head> <body> <center> <ul class="unorderedList"> <li class="listItem"> <a class="link" href="#"> Home </a> </li> <li class="listItem"> <a class="link" href="#"> About </a> </li> <li class="listItem"> <a class="link" href="#"> Contact </a> </li> <li class="listItem"> <a class="link" href="#"> Services </a> </li> <li class="listItem"> <a class="link" href="#"> Portfolio </a> </li> </ul> <h2 class="heading"> GeeksforGeeks </h2> <h3> Click the menu items to navigate through the different pages. </h3> <h4 id="result"></h4> </center> <!-- jQuery CDN Link --> <script src= "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js" integrity= "sha512-v2CJ7UaYy4JwqLDIrZUI/4hqeoQieOmAZNXBeQyjo21dadnwR+8ZaIJVT8EE2iyI61OV8e6M8PP2/4hpQINQ/g==" crossorigin="anonymous" referrerpolicy="no-referrer"> </script> <!-- jQuery script --> <script> $(document).ready(()=>{ $('.listItem').on('click', function(){ $('#result'). text(`This is the ${$(this).text()} page.`); $('.listItem').removeClass('activeLink'); $(this).addClass('activeLink'); }); }); </script> </body> </html> Output: Comment More infoAdvertise with us Next Article How to Use jQuery $(this) in Event ? A abhish8rzd Follow Improve Article Tags : JQuery Similar Reads How to use .on and .hover in jQuery ? jQuery .on() method: This method is used to attach an event listener to an element. This method is equivalent to the addEventListener in vanilla JavaScript. Syntax: $(element).on(event, childSelector, data, function) Parameter event: It specifies the event to attach (click, submit, etc.). childSelec 2 min read How to check whether the event namespace is used in jQuery ? The jQuery is lightweight, and it has a lot of features like HTML/DOM manipulation, CSS manipulation, HTML event methods, effects and animations, AJAX, utilities. Many multinational companies like Google, IBM, and Microsoft use the jQuery library in their application. The event.namespace property re 1 min read How to run a code on mouseenter event in jQuery ? In this article, we will see how to run the code when the mouse enters into the specific area using jQuery. To run the code on a mouse enter a specific area, mouseenter() method is used. The mouseenter() method works when the mouse pointer moves over the selected element. Syntax: $(selector).mouseen 1 min read How to run a code on click event in jQuery ? In this article, we will see how to run the code after clicking on an event. To run the code after clicking an event, we use the click() method. The click() method is used to run the code when a click event occurs. The click() method to attach a click event handler to selected elements. Inside the h 1 min read How to run a code on hover event in jQuery ? In this article, we will see how to change the style of elements on hover event using jQuery. To change the style of hover event, hover() method is used. The hover() method is used to specify two functions to start when the mouse pointer moves over the selected element. Syntax: $(selector).hover(Fun 1 min read Like