0

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 ?

Joab
  • 15
  • 8
  • There are already threads about element long press. Is this the sort of behavior you're looking for? http://stackoverflow.com/questions/14586883/how-to-detect-a-long-press-on-a-div-in-jquery – hodgef Nov 17 '15 at 14:51
  • 1
    Sim, acho que é isso – Joab Nov 17 '15 at 20:11

1 Answers1

0

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)

Community
  • 1
  • 1
jaredrethman
  • 512
  • 5
  • 16
  • The OP wants to detect a long press, not a click/tap. – hodgef Nov 17 '15 at 14:52
  • I'm using now this mousedown I did a combination between mousedown and mouseup , I make a counting of time, for example if I clic on the element I start to count and when the mouse leaves the element depending on the time the function is fired – Joab Nov 17 '15 at 17:31
  • Yup, that's the way to go. If you need another example feel free to consult the thread I linked to your question. – hodgef Nov 17 '15 at 21:18
  • I found this site http://ngryman.sh/jquery.finger/. there are lots of components there, including press , so this event works as I wanted – Joab Nov 19 '15 at 13:25