Sid Ngeth's Blog A blog about anything (but mostly development)

Javascript click vs touch events

If your application needs to respond to both mobile touches and desktop clicks you could write the following

function bind_button(selector, subselector) {
  $(selector).on('click', subselector, function(event) {
    handleEvent();
    return false;
  });

  if('touchstart' in window) {
    $(selector).on('touchstart', subselector, function(event) {
      handleEvent();
      return false;
    });
  }
}
comments powered by Disqus