How to attach a method to HTML element event using jQuery ? Last Updated : 26 Jul, 2024 Comments Improve Suggest changes Like Article Like Report In this article, we are going to learn, how can we attach a method to an HTML element event using jQuery.There are various events like click(), mouseenter(), mouseout(), etc which are available in jQuery. For attaching a method to an HTML elements event, we will need a jQuery method on(), which will help us to attach an event handler to the selected HTML.on(): This method is used to attach event handlers to the currently selected HTML elements. On the event handler, we can add the method and perform tasks accordingly.Syntax:.on( <events> , "<selector>" , <method> )Parameter:event: It accepts one or more jquery events separated by space.selector: It can accept the selector to filter out the children of the selected element, if the selector is not passed then it only works on the selected element. method: With the help of the method, we can pass the data to the respective event when that event is fired.CDN link: Add the given link below to the head tag of the HTML file to work with jQuery.<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>Example 1: In this example, we will add a method to the HTML div with the help of the on() method, that will change the color of the text inside the div. HTML <!DOCTYPE html> <html lang="en"> <head> <title> Attaching method to HTML element</title> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"> </script> <style> .box { height: 200px; width: 200px; border: 1px solid black; display: flex; justify-content: center; align-items: center; } </style> </head> <body> <div class="box"> <h3>GeeksforGeeks</h3> </div> <script> $(document).ready(function(){ $(".box").on("click", function(){ $(".box").css("color", "green"); }) }); </script> </body> </html> Output:Example 2: In this example, we will attach a method to the div directly to its tag. We can call onmouseenter() event so that when the mouse will enter the HTML div, the size of the div and the size of the font will get smaller as well as the previous method will also work. When we click on the div, the color of the text will also be changed to green. HTML <!DOCTYPE html> <html lang="en"> <head> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"> </script> <style> .box { height: 200px; width: 200px; border: 1px solid black; display: flex; justify-content: center; align-items: center; } </style> </head> <body> <div class="box" onmouseenter="message();"> <h3>GeeksforGeeks</h3> </div> <script> $(document).ready(function () { $(".box").on("click", function () { $(".box").css("color", "green"); }) }); function message() { $(".box").animate({ height: "100px", width: "100px", }); $("h3").css("font-size", "15px"); } </script> </body> </html> Output: Comment More infoAdvertise with us Next Article How to attach a method to HTML element event using jQuery ? iamgaurav Follow Improve Article Tags : Technical Scripter Web Technologies JQuery Technical Scripter 2022 jQuery-Events jQuery-Questions +2 More Similar Reads How to create an HTML element using jQuery ? In this article, we will see how to create an HTML element using jQuery. To create and append the HTML element, we use the jQuery append() method. The jQuery append() method is used to insert some content at the end of the selected elements. Syntax: $(selector).append( content, function(index, html) 2 min read How to get the outer html of an element using jQuery ? Sometimes, there is a need to get the entire HTML element by its id and not merely its contents, for doing so, we shall use the HTML DOM outerHTML Property to get the outer HTML of HTML element. Syntax: document.getElementById("your-element-id").outerHTML) You can use a variable and initialize it to 2 min read How to append an element in two seconds using jQuery ? Given an HTML element and the task is append an element in the document after few seconds using fadeIn effect with the help of JQuery. Approach: Select the target element to append the element. Use one of the appendTo() or append() method to append the element. Example 1: In this example, the <di 2 min read How to use jQuery to Search and Replace HTML elements? Using jQuery, you can dynamically search and replace HTML elements on a webpage. By selecting elements and binding actions to events, you can manipulate their content or structure in response to user interactions or other triggers ApproachIntegrate the jQuery library hosted on a content delivery net 2 min read How to find the class of clicked element using jQuery ? In this article, we will find the class of the clicked element using jQuery. To find the class of clicked element, we use this.className property. The className property is used to set or return the value of an elementâs class attribute. Using this property, the user can change the class of an eleme 1 min read How to bind an event on an element but not on its children in jQuery ? In this article, we will learn how to bind an event on an element but not on its children in jQuery. We want to add an event on the parent element but not on the child element. Approach: We can do this task in the following steps: First, we add a click event on the div element that contains a paragr 2 min read How to add class to an element without addClass() method in jQuery ? The task is to add a new class to the element without using addClass() method with the help of jQuery. There are two approaches that are discussed below: Approach 1: To perform the operation, we can use toggleClass() method to toggle the class which we want to add to the element. Example: HTML <! 2 min read How to Add Attributes to an HTML Element in jQuery? To add attributes to an HTML element in jQuery, the attr() method can be used. This allows to set one or more attributes for a selected element, such as id, class, src, and href.Approach: Given an HTML document containing <label>, <input>, and <button> elements. The <input> e 1 min read How to create hidden form element on the fly using jQuery ? JQuery is the library that makes the use of JavaScript easy. Inserting the <input type='hidden'> can also be done using jQuery. The append() and appendTo() inbuilt function can be used to add the hidden form element but they are not only limited to the <input type='hidden'> but we can al 2 min read How to use hide() method on button click using jQuery ? jQuery has a lot of handy methods to get the work done easily. In this article, we will discuss one of them which is the hide() method. We can use this method for various purposes on our webpage and get an efficient result. The very first step will be creating an HTML file and link the jQuery librar 2 min read Like