YouTip LogoYouTip

Jquerymobile Events Touch

# jQuery Mobile Touch Events Touch events are triggered when a user interacts with a touch-sensitive screen (such as a smartphone or tablet). > **Note:** jQuery Mobile touch events are also fully compatible with desktop environments. Users can trigger these events using a standard mouse click or drag! --- ## 1. Tap Event (`tap`) The `tap` event triggers when a user taps an element. It is the mobile equivalent of a standard click event but is optimized for mobile responsiveness by eliminating the default 300ms click delay. ### Example The following example hides a `

` element when it is tapped: ```javascript $("p").on("tap", function(){ $(this).hide(); }); ``` --- ## 2. Tap and Hold Event (`taphold`) The `taphold` event triggers when a user taps and holds their finger on an element for an extended period (approximately one second) before releasing. ### Example The following example hides a `

` element when a user performs a tap-and-hold action on it: ```javascript $("p").on("taphold", function(){ $(this).hide(); }); ``` --- ## 3. Swipe Event (`swipe`) The `swipe` event triggers when a user drags their finger quickly across an element. By default, a swipe is recognized when the user drags horizontally by more than **30px** and vertically by less than **20px** within a **1-second** duration. ### Example The following example updates the text of a `` element when a swipe gesture is detected on a `

` element: ```javascript $("p").on("swipe", function(){ $("span").text("Swipe detected!"); }); ``` --- ## 4. Swipe Left Event (`swipeleft`) The `swipeleft` event triggers specifically when a user drags their finger across an element from right to left by more than **30px**. ### Example The following example displays an alert box when a user swipes left on a `

` element: ```javascript $("p").on("swipeleft", function(){ alert("Swiped left!"); }); ``` --- ## 5. Swipe Right Event (`swiperight`) The `swiperight` event triggers specifically when a user drags their finger across an element from left to right by more than **30px**. ### Example The following example displays an alert box when a user swipes right on a `

` element: ```javascript $("p").on("swiperight", function(){ alert("Swiped right!"); }); ``` --- ## Developer Considerations & Best Practices When working with jQuery Mobile touch events, keep the following tips in mind: * **Event Delegation:** For dynamically generated elements, use event delegation to ensure touch events are bound correctly: ```javascript $(document).on("tap", ".dynamic-element", function() { // Your code here }); ``` * **Preventing Default Behavior:** In some mobile browsers, swiping may trigger page scrolling. You can prevent this default behavior by using `event.preventDefault()` inside your event handler: ```javascript $("p").on("swipe", function(event){ event.preventDefault(); // Swipe logic }); ``` * **Customizing Thresholds:** You can customize the default swipe thresholds globally by modifying the `$.event.special.swipe` object properties, such as `scrollSupressionThreshold` (default 10px), `durationThreshold` (default 1000ms), `horizontalDistanceThreshold` (default 30px), and `verticalDistanceThreshold` (default 30px).

← Jquerymobile Events ScrollJquerymobile Events β†’