Search code examples
jquerytouchstart

detect touchstart only if element has a specific class


I am listening for an event with JQuery as follows:

$(document).on('click touchstart', '.open-qv', function(e){
      e.preventDefault(); 

However I only want to listen for/detect touchstart if the element touched has a specific class eg. .hotspot If it doesn't have this class I only want to listen for click, is there a way to do this?


Solution

  • You should use event.type to check that either event is not touchstart or element .hasClass() hotspot:

    $(document).on('click touchstart', '.open-qv', function(e){
      if(event.type!="touchstart" || $(this).hasClass('hotspot') ){
         //do stuff
      }