How to perform click-and-hold operation inside an element using jQuery ? Last Updated : 31 Jul, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report Given an HTML element and the task is to click and hold an element in a document for a few seconds to perform the operation using jQuery. Approach: Select an HTML element. Add the event listener to that element and add a timeOut timer for that event. If that event happens to active for few seconds then trigger other event to identify that the event happens. Example 1: In this example, clicking and holding inside the div for 2 seconds will trigger other event, which simply prints that event happened. html <!DOCTYPE HTML> <html> <head> <title> How to perform click-and-hold operation inside an element using jQuery ? </title> <style> #div { background: green; height: 100px; width: 200px; margin: 0 auto; color: white; } </style> <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"> </script> </head> <body style = "text-align:center;"> <h1 style = "color:green;"> GeeksForGeeks </h1> <p id = "GFG_UP" style = "font-size: 19px; font-weight: bold;"> </p> <div id = "div"> Div Element. </div> <br> <p id = "GFG_DOWN" style = "color: green; font-size: 24px; font-weight: bold;"> </p> <script> $('#GFG_UP').text("Click and Hold inside the" + " div for 2 seconds"); var tId = 0; $('#div').on('mousedown', function() { tId = setTimeout(GFG_Fun, 2000); }).on('mouseup mouseleave', function() { clearTimeout(tId); }); function GFG_Fun() { $('#GFG_DOWN').text("Click and Hold for 2 " + "seconds, done."); } </script> </body> </html> Output: Before clicking on the button: After clicking on the button: Example 2: In this example, clicking and holding inside the body document for 2 seconds will trigger other event, which simply prints that event happened. This example separates the logic of mousedown and mouseup events. html <!DOCTYPE HTML> <html> <head> <title> How to perform click-and-hold operation inside an element using jQuery ? </title> <style> #div { background: green; height: 100px; width: 200px; margin: 0 auto; color: white; } </style> <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"> </script> </head> <body style = "text-align:center;"> <h1 style = "color:green;"> GeeksForGeeks </h1> <p id = "GFG_UP" style = "font-size: 19px; font-weight: bold;"> </p> <div id = "div"> Div Element. </div> <br> <p id = "GFG_DOWN" style = "color: green; font-size: 24px; font-weight: bold;"> </p> <script> $('#GFG_UP').text("Click and Hold inside the" + " div for 2 seconds"); var tId = 0; $("#div").mousedown(function() { tId = setTimeout(GFG_Fun, 2000); return false; }); $("#div").mouseup(function() { clearTimeout(tId); }); function GFG_Fun() { $('#GFG_DOWN').text("Click and Hold for 2 " + "seconds, done."); } </script> </body> </html> Output: Before clicking on the button: After clicking on the button: Comment More infoAdvertise with us Next Article How to hide/show an image on button click using jQuery ? P PranchalKatiyar Follow Improve Article Tags : JQuery jQuery-Misc Similar Reads How to move one DIV element inside another using jQuery ? In this article, we will learn to move one HTML div element inside another using jQuery. The HTML div element is used to define a division or a section in an HTML document.Method used: parent(): This method is used to get the parent of each element in the current set of matched elements, optionally 2 min read How to hide/show an image on button click using jQuery ? In this article, we will see how we can hide or show any particular image in jQuery when a button gets clicked. This is quite easy to do with some lines of jQuery code. Before we jump to the topic, let's know which methods of jQuery will be used for this. So there is a method called show() and anoth 3 min read How to click a button to animate the paragraph element using jQuery ? In this article, we will click a button to animate the paragraph element using jQuery. To animate the paragraph elements, we use animate() method. The animate() method is used to change the state of the element with CSS style. This method can also be used to change the CSS property to create the ani 1 min read How to click anywhere on the page except one element using jQuery ? A web-page contains many elements and the task is to click anywhere on the page except one element using jQuery. There are two methods to solve this problem which are discussed below: Approach 1: This approach calls a function when a click event happens.First check the id of the targeted element and 3 min read How to attach a method to HTML element event using jQuery ? 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 2 min read How to create a pop-up div on mouse over and stay when click using jQuery ? In this article, we will learn how to create a pop-up div on mouseover and stay when click using jQuery. Approach: First, we create an HTML div element that we want to pop up when we mouse over on an element and set its display property to none in CSS style.display:none;In the script tag, we create 2 min read Like