0

I would like to target the element clicked using $(this) however I need this code to be within a function like this:

function openQv(){
      var  lookImg = $(this).parent().find(".look-img").attr("src");
}

$(document).on('click touchstart', '.hotspot', function(e){
      e.preventDefault(); 
      openQv();
});

Using $(this) within an external function doesn't seem to work, is there a way to do this?

user1937021
  • 9,133
  • 21
  • 75
  • 136

1 Answers1

5

Reference the function and it works out of the box

function openQv(e){
    e.preventDefault(); 
    var  lookImg = $(this).parent().find(".look-img").attr("src");
}

$(document).on('click touchstart', '.hotspot', openQv);

Or you could set the this value using call, apply or bind

$(document).on('click touchstart', '.hotspot', function(e){
      e.preventDefault(); 
      openQv.call(this);
});
adeneo
  • 303,455
  • 27
  • 380
  • 377