I have the following widget
var a = $('#test').timetable({
cell_click: openDialog
});
whereby cell_click is an event generated by
_create:function(){
dayBodyCells.click(function(){
if( !$(this).hasClass('cell-inactive') ){
var dic = self.getElementPos(this);
self._trigger('cell_click', null,dic);
}
});
and openDialog is the callback function. In the callback function for the dayBodyCells, i have this equaling the td element, which is what i expected. I’m curious – why does this inside function openDialog instead refers to #test?
Within a bound event handler (callback),
thisrefers to the element on which the event was triggered. So:In your code, dayBodyCells must be a td (as you expect) therefore
thisrefers to it in theclickhandler. However, when you trigger thecell_clickevent, you must be firing it from the#testelement (viaself._trigger).If
self._trigger('cell_click', null,dic)were replaced with$(this).trigger('cell_click', null,dic),thiswould then refer to thetdwithinopenDialogHave a look at http://www.pkshiu.com/loft/archive/2009/01/understanding-this-this-and-event-in-a-jquery-callback-function and http://api.jquery.com/category/events/