Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if click was triggered by touch or click

I have function that works on click

$('#test').click(function(e){
  // some code
});

How can I check if the test element clicked or touched?

like image 423
Alvarez Avatar asked Apr 04 '16 17:04

Alvarez


1 Answers

You could use one event for the both then detect type of the event triggered :

$('#test').on('touchend click',function(e){
  if(e.type=='click')
      alert('click triggered');
  else
      alert('touch triggered');
});

Hope this helps.


$('#test').on('touchend click',function(e){
  if(e.type=='click')
    alert('click triggered');
  else
    alert('touch triggered');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="test">TEST</button>
like image 128
Zakaria Acharki Avatar answered Sep 20 '22 18:09

Zakaria Acharki