I have an array of classes in javascript, each instance is created passing it the name of an mp3file. the idea is that each instance creates an "invisible" audio element and a div. When you click on the div you here it's audio.
This is a simplified version of the class... Every attempt I have tried to pass a pointer to the appropriate class to the "onclick" function has failed.. "this" obviously fails, the example below fails.. I have provisionally solved the problem by passing the class an "idx" and then to the div.onclick, so it "find's itself", but I am sure there is a more elegant solution of passing the div a reference to the appropriate audio element to play.
function myClass (mp3file) {
this.aud = document.createElement('audio');
this.aud.src = mp3file;
this.div = document.createElement('div');
this.div.className = 'dictDiv';
this.context = this;
this.div.onclick = function () {
context.aud.play();
}
}