I have a class in javascript with a function:
function myClass()
{
this.myInit = function()
{
myObj = this;
var id-foo = 'myId';
jQuery('#'+id-foo).bind('click', function() { myObj.foo(id-foo); } );
}
this.foo = function(id) { alert(id); }
}
I want just to bind a DOM element to an event (click, dragonstart etc.) and make it call a function of the current object (actually this). I’ve tried to call this.foo directly but that doesnt work, firebug tells me that foo is not a function. Is the parameter id-foo which I give to the anonymous function the problem?
Thx in advance
Your problem is here :
This isn’t a valid name for a variable.
Note also that your code won’t work if you don’t call the
myInitfunction on an instance ofmyClass.This, for example, wouldn’t work :
while this one should work :
DEMONSTRATION