How to perform click-and-hold operation inside an element using jQuery ? Last Updated : 31 Jul, 2019 Comments Improve Suggest changes 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 perform click-and-hold operation inside an element using jQuery ? P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies 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 How to make Mini Form element Inline 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 Mini Form element Inline button using jQuery Mobile. Approach: First, add jQuery Mobile scripts needed for your project. <link 1 min read How to add a title in anchor tag using jQuery ? In this article, we will see how to add a title in the anchor tag using jQuery. The title property is used to specify extra information about the element. When the mouse moves over the element then it shows the information. The prop() method is used to add properties and values to the element using 2 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 disable right-click option using the jQuery ? The bind() method in jQuery is used to attach one or more event handlers for selected element and this method specifies a function to run when an event occurs. Syntax: $(selector).bind(event, data, function);Parameters: This method accepts three parameters as mentioned above and described below: eve 2 min read Like