I have this jQuery plugin:
$.fn.touchBind = function(func) {
$(this).live('touchmove', function() {
$(this).addClass('dragged');
});
$(this).live('touchend', function() {
if ($(this).hasClass('dragged') == false) {
func();
}
});
return this;
}
and call it like so:
$('.the-element').touchBind(function() {
$(this).hide();
});
My problem is that $(this) in $(this).hide() doesn’t refer to $('.the-element'), but rather DOMWindow. Is there a way I can make this work?
Change
func();tofunc.call(this);or$.proxy(func, this)();.You could also use
apply()(not necessary whencall()suits) orbind()(limited browser support,$.proxy()does essentially the same thing).