I'm working on a project right now which is primarily front end UI.
Right now i'm encountering an issue with one of my functions, in which the return value is returning the function itself and not the production of the function.
I'll first start by explaining the expected results:
1.) There is a menu on the page, when clicking on a button in the menu another menu is added to the page with it's own buttons - i'll refer to this as the sub menu.
2.) When clicking the button available in the sub menu, another menu is create and added to a list elsewhere on the page.
I'll explained the code that I have, as well as where the issue lies below:
1.) Here is where the menu button is created:
var add = document.createElement('button');
2.) Here is where I add the event listener to the button above:
closure = (function(arrayItem)
{
var test = function(event)
{
object.spaces.push(selectedSpace.id);
object.spaceobjects.push(selectedSpace);
var enemyPanel = addEntityPanel(roomnumber, selectedSpace.id, object, object.savelocation, true);
return enemyPanel ;
};
return test;
})(arrayItem);
add.addEventListener("click", closure, false);
I used the variable closure and passed it an anonymous function for closure reasons, this way the variable arrayItem can be modified through a for loop.
You may notice the function addEntityPanel, this function creates the menu that I've been referring to and adds it to the list, it also returns an object with all of the html elements used to create the menu - storing it in the variable enemyPanel.
enemyPanel from the test function is returned to the variable test and test is returned to the variable closure which is supposed to give me access to the object enemyPanel created in the addEntityPanel function; however all that is being returned is the function itself as a string. I've tried to change:
return test;
to
return test();
But all this does is execute the function when the button in the sub menu is created. I only want it to be executed when the button is clicked.
I'm not too familiar with closure, so if there's something wrong with the closure function preventing it from generating the desired output - I would not be surprised.
I'm open to any and all suggestions. Please let me know what you guys think, thank you!
enemyPanel? You can't do anything with it inclosure; by the time a click occurs,closurehas already returned.