Update script.js

Avoid creating a function inside a loop. Solve incorrect event listener.
Merge pull request #2 from elperi/patch-1

Update script.js