0

I have the following function in javascript that adds div DOM objects to an Interface object called pageDivs.

FCInterface.prototype.initializePageDivs = function() {
    this.pageDivs.body = document.getElementsByTagName('body')[0];
    this.pageDivs.mainMenu = document.getElementsByClassName('mainMenu')[0];
    this.pageDivs.avatarMenu = document.getElementsByClassName('avatarMenu')[0];
};

The goal here is when this method is called, I have an object self.pageDivs with a bunch of object key to element list of DOM items I can use later in my application. But instead of manually calling each one by one, I think it would be smarter to add these to an object and iterate over them when adding the DOM objects. Something like

FCInterface.prototype.initializePageDivs = function() {
    var d = {
        body:document.getElementsByTagName('body')[0],
        mainMenu:document.getElementsByClassName('mainMenu')[0],
        avatarMenu:document.getElementsByClassName('avatarMenu')[0]
    },
        self = this;

    for (var k in d) {
        var dd = 'self.pageDivs.'+k;
        dd = d[k];
    }
    // this.pageDivs.body = document.getElementsByTagName('body')[0];
    // this.pageDivs.mainMenu = document.getElementsByClassName('mainMenu')[0];
    // this.pageDivs.avatarMenu = document.getElementsByClassName('avatarMenu')[0];
};

But I am having an issue trying to add the string self.pageDivs. to the element k in my iteration. When I do 'self.pageDivs.'+k it is not working. What am I missing?

Zach Smith
  • 5,490
  • 26
  • 84
  • 139

0 Answers0