move click event handler inside function