So i have below module created according to an example i found.
I initialize it with
equipment_booking = new equipment({
equipment: '#equipment_order',
type: '#equipment_type_order',
validation: '#equipment_validation_order',
submit: "#submit_booking"
});
What I want to do is access the typeVar
from outside the module. I found out that I need to use the this.
in front of it. And indeed I could access it. However I can’t seem to access it inside the module functions. How would I accomplish this? This is a part of the module I have written. I have tried different ways in the checkInput function. Any help or suggestions are realy appriciated.
var equipment = (function() {
function equipment(options) {
// Set the standard options
var o = {
equipment: null,
type: null,
validation: null,
submit: null
};
// Get some other needed variables
number = "";
this.typeVar = 0;
var error = [{
code: 0,
msg: ""
}];
// Then we can override the given options to the standard options
for (var k in options) {
if (options.hasOwnProperty(k)) {
o[k] = document.querySelector(options[k]);
}
}
// Add the eventlisteners
o.equipment.addEventListener('keydown', checkInput);
o.equipment.addEventListener('blur', validateInput);
o.type.addEventListener('change', validateInput);
function checkInput(e) {
console.log("CheckInput called"); // This console.log is called
console.log(typeVar); // Error: typeVar is not defined
console.log(this.typeVar); // Undefined
e.preventDefault(); // The e works
}
function validateInput() {
// Validating input
}
}
return equipment;
})();