moved click event out of loop