Javascript works, just not in the function I need it to work in