Move key variable inside on click function