To improve our code and ensure its all contained and will not be effected by other code we write. For this example we have something like this using document.ready and several functions. These are all available globally and on the window. So we need to use some methods to clean up our code.
This is our code that needs some cleaning up
In this example we are creating a modal using an a linked html file. The functionality is not that important its just so we can get an idea of how this process works.
Here are the steps for this example
Surround our code in a self invoking function.
I have moved the click handler function into its own function and then called on the event
Create an init function and place our calls and variables in here
Then to complete the closure I have returned the init
Move the code initialisation to the bottom
The final code