How to perform click-and-hold operation inside an element using jQuery ?
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.
<!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 = </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.
<!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 = </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:
Recommended Posts:
- How to enable/disable all input controls inside a form element using jQuery ?
- CSS to put icon inside an input element in a form
- How to remove parent element except its child element using jQuery ?
- jQuery | Move an element into another element
- jQuery | element + next Selector
- jQuery | element Selector
- How to select an element by name with jQuery?
- jQuery | Create a div element
- How to get focused element using jQuery?
- How to create a div element in jQuery ?
- How to change the element id using jQuery ?
- How to hide div element after few seconds in jQuery ?
- How to append an element in two seconds using jQuery ?
- JQuery | Get the n-th level parent of an element
- How to add options to a select element using jQuery?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



