Oh dear, it seems that you just donât understand. There is a different way to deal with the duplication of the callback function, and that is from the initial code at https://jsfiddle.net/kcr3n01e/ to just delete the whole createCallback function.
The next part is where we give a cover to the manageUI code and get a player in return.
Before we can do that, we first need a cover. The addPlayer code uses coverSelector to get a cover, and then from that it gets the parentElement.
We need to split that into two different lines. One line defines a cover variable where querySelector and coverSelector are used to get the cover, and the other line defines a parent variable that uses the cover variable and parentElement.
When you have the cover variable being reliably defined, we can then move on to doing the manageUI stuff.
We now need to extract that code out to a separate function in manageUI, so that we replace that code in the addPlayer function with just the following get the wrapper:
Right at the top. There tends to be an ordering preference with functions, where the more generic functions are higher up than the more specific functions.
In the getWrapper function, you have for some reason used evt for the function parameter.
That is wrong. getWrapper is not an event handler. Itâs not used by any event-handling code at all.
Can you please help me to understand why you used evt there for the function parameter.
The getWrapper function is creating a wrapper variable, but nothing is being done with it. That wrapper needs to be returned at the end of the function.
The manageUI code now has a working getWrapper function which uses the cover to get the wrapper.
Currently getWrapper goes up to the coverâs parent and searches for the wrapper. A different technique needs to be used for this to work with the other HTML structure that you have.
That other technique involves having manageUI keep a list of wrappers, and a list of covers.
The init function can run a separate function that searches for those wrappers and covers.
There will be the same number of wrappers and covers. That way, we can search the covers to find the index number of the cover, and return the same index number from the wrappers.
Thatâs the overall idea, for it will work with all known types of HTML structure that you use.
Do you have you any questions before we carry on?
Good one. We will be adding objects to a players array that contains both wrapper and cover properties. We will end up with players items in the array similar to {wrapper: âŚ, cover: âŚ} where the wrapper and cover properties each refer to the different HTML elements for each player.
Create a players array at the top of the manageUI function. Then at the top of the init function add a function call to findPlayers();
That function doesnât exist yet, but we will be using it to populate the players array.