Is it possible to implement "long press" in JavaScript (or jQuery)? How?
alt text http://androinica.com/wp-content/uploads/2009/11/longpress_options.png
HTML
<a href="" title="">Long press</a>
JavaScript
$("a").mouseup(function(){
// Clear timeout
return false;
}).mousedown(function(){
// Set timeout
return false;
});
There is no 'jQuery' magic, just JavaScript timers.
You could set the timeout for that element on mouse down and clear it on mouse up:
With this each element gets its own timeout.
For me it's work with that code (with jQuery):
While it does look simple enough to implement on your own with a timeout and a couple of mouse event handlers, it gets a bit more complicated when you consider cases like click-drag-release, supporting both press and long-press on the same element, and working with touch devices like the iPad. I ended up using the longclick jQuery plugin (Github), which takes care of that stuff for me. If you only need to support touchscreen devices like mobile phones, you might also try the jQuery Mobile taphold event.
You can use jquery-mobile's taphold. Include the jquery-mobile.js and the following code will work fine
You can use taphold event of jQuery mobile API.