moves click event outside of loop