When in doubt, add more JavaScript. And then add some more, just in case.
Step 1: Write 5x as many lines of code as necessary to accomplish the task.
Step 2: Realize the code doesn't work and add another 5x the amount of lines.
Step 3: Repeat steps 1 & 2 until the desired result is achieved.
Disclaimer: The views expressed herein are entirely fictional and not intended to be taken seriously.