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
  • 10,151
  • 22
  • 81
  • 143

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
  • 312,895
  • 29
  • 395
  • 388