Search code examples
jquerymobilemousepress

How to create a mouse press in Jquery developing an application to Mobile?


I'm new in Jquery.

I'm developing a mobile application with HTML5 + CSS and JQUERY,

If I go to develop an application for Android with native code, for example: I can use a function on listview, when the user presses for some seconds an action happens , so I'd like to do same event , but with jquery.

I have this div

<div class="perform-mouse-press"> hsjashja </div>

When I click on it I would like to do something like this :

 //Holding some seconds , but the user is pressing the div
$(".perform-mouse-press).mousepress(function(){

     //Doing something...

  });

Has somebody any idea how can I do it ?


Solution

  • Your question is a little vague, but I'll give it a go...

    Your most likely just look for a click event - https://api.jquery.com/click/

    $('.perform-mouse-press').click(function (){
            //launch rocket ship
    });
    

    Even better is to use the on method (way better for binding and unbinding) - http://api.jquery.com/on/

    $('.perform-mouse-press').on('click',function (){
            //launch rocket ship
    });
    

    Alternatively if your looking to capture any (left, right or middle) mouse click/down events you could look at using mousedown which is used exactly the same as click and can, and should be, used in an on () method to bind to an element - https://api.jquery.com/mousedown/

    EDIT: @Franciscos H link to - How to detect a long press on a div in Jquery? is the correct way to detect a long click, personally I prefer the implementation using a combination of mousedown, mouseup and setTimout (below the accepted answer)