I have a little problem. I've created some element in an object and that works fine. But now I want to add some EventListeners to execute a function inner my object, the problem is that I cannot execute the function "setSelection". ("this" is not the "this" of the object, but the "this" of the event inner the click-function).
function Object() {
this.init = function () {
// delete table
// ...
// set table
$('tr').append('<td id="1">test</td>');
$('td').click(function() {
// execute setSelection
});
}
this.setSelection = function(x) {
alert('You clicked id ' + x);
}
}
$(document).ready(function() {
x = new Object();
x.init();
});
Objectfunction, if you know what you are doing, fine, or else, you should rename it. For your problem, what causes this is that event callbacks are executed in the global context. This is why the proposed answers work, you need either to store the context in a var, or use functions asbind,applyor such to get the context back