0

If a function inside an event listener is called, say for instance, window.load, are all other functions inside said function expected to be ran on load as well? I have an issue with a function being called on window load and that function calling another function. At the last step I want to add an event listener to watch for form submission. However the listener function has no access to the the variables called from the original load listener function.

var Handler = {
    
    ajaxURL: null,

    domIDs : null,
    
    httpRequest : null,
    
    init: function(config) {
        this.httpRequest = {};
        this.initConfig(config);
        this.setLoadHandler();
    },
    
    initialize: function() {
    this.initializeDocumentNodes();

    },
    
    setLoadHandler: function(){
        var self = this;
        this.addEvent(window,'load', function() {
            self.initialize();
        });
    },
    
    initializeDocumentNodes: function() {
        this.dom = {};
        for(var key in this.domIDs) {
            this.dom[key] = document.getElementById(this.domIDs[key]);
        }
        
        this.dom['loginForm'].addEventListener("submit", function(ev) {
            ev.preventDefault();
            console.log(this.dom['userNameLoginInput']);
            console.log(this.dom['passwordLoginInput']);
            
        });
        
    },


this.dom is undefined inside the form submit listener.

  this.dom['loginForm'].addEventListener("submit", function(ev) {
            ev.preventDefault();
            console.log(this.dom['userNameLoginInput']);
            console.log(this.dom['passwordLoginInput']);

        });

Could someone please help me to understand this? Thank you so much.

Justin Harris
  • 43
  • 1
  • 7
  • Create the callback function elsewhere and bind `this` to it before passing it to the addEventListener. – evolutionxbox Jul 30 '20 at 18:49
  • The variables do exist since I tested it outside of the submit event listener – Justin Harris Jul 30 '20 at 18:49
  • 1
    I don't think you understand how `this` works in JS. `this` changes depending on _how_ the function is called. – evolutionxbox Jul 30 '20 at 18:50
  • 2
    Do it the same way you did it in `setLoadHandler` -- declare the `self` variable and then use `self.dom` – Barmar Jul 30 '20 at 18:50
  • Or use arrow functions, which essentially replace the `var self = this;` hack – Robin Zigmond Jul 30 '20 at 18:51
  • Okay thanks for the information. Yeah I am a little bit unfamiliar with `this` in JS. – Justin Harris Jul 30 '20 at 18:54
  • @JustinHarris that's fair enough. We all start not knowing. I recommend reading https://github.com/getify/You-Dont-Know-JS/blob/1st-ed/this%20&%20object%20prototypes/README.md#you-dont-know-js-this--object-prototypes – evolutionxbox Jul 30 '20 at 18:57
  • I fixed it by assigning this to var self outside the listener according to your suggestions and this article https://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-inside-a-callback I will check out the link aswell. Thanks very much <3 – Justin Harris Jul 30 '20 at 19:22

0 Answers0