moved js code to inside event listener