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
  • 8,155
  • 20
  • 68
  • 129

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
  • 293,187
  • 26
  • 361
  • 361