HTML
<input type="checkbox" class="test" id="chk" /><label for="chk">Click</label>
<p id="demo"></p>
JavaScript
(function (){
var chk = document.querySelector('.test');
function ClickTest(elem){
this.elem = elem;
};
ClickTest.prototype.addListener = function() {
this.elem.addEventListener('change', this.showValue);
};
ClickTest.prototype.showValue = function(){
console.log('Clicked');
var d = document.getElementById('demo');
d.innerHTML = "This works";
this.test();
};
ClickTest.prototype.test = function(){
console.log('Test function');
};
clickTest = new ClickTest(chk);
clickTest.addListener();
})();
My code works fine before this.test() is called. The error I get is
TypeError: this.test is not a function
Is it because showValue is called from addEventListener or am I doing something wrong here?