I have got a class, I make a new instance of this class for two elements.
When clicked, these elements should then add a class to another element, however the console.log
is displaying undefined
, any ideas why?
// Wait for document to load
window.addEventListener("DOMContentLoaded", function() {
class menuControl {
constructor(obj, child) {
this.state = 0;
this.child = child;
this.elm = obj;
}
toggleActive() {
if (this.state === 1) {
this.state = 0; removeClass(this.child, 'toggled');
} else {
console.log(this.child);
this.active = 1; addClass(this.child, 'toggled');
}
}
}
// Global Variables
var usedcars = new menuControl(doc('usedcars__trigger'), doc('listitem__search'));
var menu = new menuControl(doc('megamenu__trigger'), doc('megamenu'));
function doc(id) { return document.getElementById(id); }
(usedcars.elm).addEventListener('click', usedcars.toggleActive);
}, false);